exercises:)xsd,)xpath)atzeni/didattica/bd/20112012/ex 02 xsd-xpath.pdf · exercises:! 1xfrom...
TRANSCRIPT
![Page 2: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/2.jpg)
Hints:
• Use a validator • XSD – Eclipse has a plugin for XML/XSD/DTD valida4on – W3C Validator: hMp://www.w3.org/2001/03/webdata/xsv
– Another Validator: hMp://www.xmlforasp.net/schemavalidator.aspx
• XPath – Guess what? There’s a plugin for Eclipse – Extensions for Firefox and Chrome
![Page 3: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/3.jpg)
Exercises: 1x From XSD to instance 2x From instance to XSD 1x From specs to XSD
![Page 4: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/4.jpg)
Exercise 1: Cara%ere From XSD to instance
![Page 5: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/5.jpg)
Ex.1 -‐ From XSD to instance: Cara%ere <?xml version="1.0"?> <xsd:schema xmlns:xsd=”
hMp://www.w3.org/2001/XMLSchema"> <xsd:simpleType name="cifra">
<xsd:restric4on base="xsd:string"> <xsd:paMern value="[0-‐9]"/> </xsd:restric4on>
</xsd:simpleType> <xsd:simpleType name="caraMere">
<xsd:restric4on base="xsd:string"> <xsd:paMern value="[A-‐Z]"/> </xsd:restric4on>
</xsd:simpleType> <xsd:element name="car" type="caraMere"/> <xsd:element name="num" type="cifra"/> <xsd:element name="esercizio" type="Tesercizio"/> <xsd:complexType name="Tesercizio">
<xsd:sequence> <xsd:element ref="car" minOccurs="6" maxOccurs="6"/> …
<xsd:element ref="num" minOccurs="2" maxOccurs="2"/> <xsd:element ref="car"/> <xsd:element ref="num" minOccurs="2" maxOccurs="2"/> <xsd:element ref="car"/> <xsd:element ref="num" minOccurs="3" maxOccurs="3"/> <xsd:element ref="car"/> </xsd:sequence>
</xsd:complexType> </xsd:schema>
![Page 6: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/6.jpg)
Ex.1 -‐ From XSD to instance: Cara%ere <xsd:simpleType name="cifra">
<xsd:restric4on base="xsd:string"> <xsd:paMern value="[0-‐9]"/> </xsd:restric4on>
</xsd:simpleType> <xsd:simpleType name="caraMere">
<xsd:restric4on base="xsd:string"> <xsd:paMern value="[A-‐Z]"/> </xsd:restric4on>
</xsd:simpleType> <xsd:element name="car" type="caraMere"/> <xsd:element name="num" type="cifra"/> <xsd:element name="esercizio" type="Tesercizio"/> <xsd:complexType name="Tesercizio">
<xsd:sequence> <xsd:element ref="car" minOccurs="6" maxOccurs="6"/> <xsd:element ref="num" minOccurs="2" maxOccurs="2"/> <xsd:element ref="car"/> …
… <xsd:element ref="num" minOccurs="2" maxOccurs="2"/> <xsd:element ref="car"/> <xsd:element ref="num" minOccurs="3" maxOccurs="3"/> <xsd:element ref="car"/> </xsd:sequence>
</xsd:complexType>
Many “styles” of XSD: • Anonymous vs Nominal types • Local vs Global elements
![Page 7: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/7.jpg)
Ex.1 -‐ Solu4on <esercizio>
<car>X</car><car>X</car><car>X</car> <car>Y</car><car>Y</car><car>Y</car> <num>1</num><num>1</num> <car>Z</car> <num>2</num><num>2</num> <car>H</car> <num>3</num><num>3</num><num>3</num> <car>K</car>
</esercizio>
![Page 8: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/8.jpg)
Exercise 2: Binario From specs to XSD
![Page 9: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/9.jpg)
Ex.2 -‐ From specs to XSD: Binario
• Define a XSD that validates XML documents describing binary strings.
• The root element <binario> contains an arbitrary sequence of
elements <uno> and <zero>, in any order.
![Page 10: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/10.jpg)
Ex.2 -‐ Solu4on <xsd:element name="binario">
<xsd:complexType> <xsd:choice minOccurs="0" maxOccurs="unbounded"> <xsd:element name="zero" type="xsd:unsignedByte" fixed="0"/> <xsd:element name="uno" type="xsd:unsignedByte" fixed="1"/> </xsd:choice> </xsd:complexType>
</xsd:element> -‐-‐-‐ <binario>
<uno>1</uno> <zero>0</zero> <zero>0</zero> <uno>1</uno> … </binario>
![Page 11: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/11.jpg)
Exercise 3: Le%era From instance to XSD
![Page 12: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/12.jpg)
Ex.3 -‐ From instance to XSD: LeMera <leMera>
Gen4le <cliente> tal dei tali </cliente>, la informiamo che i seguen4 ar4coli da lei ordina4 non sono più disponibili a magazzino: <ordine num="1234">
<ar4colo> <codice>1</codice> <descr>ar4colo 1</descr>
</ar4colo> <ar4colo>
<codice>5</codice> <descr>ar4colo 5</descr>
</ar4colo> </ordine> Cordiali salu4, <responsabile><4t>dr.</4t>Mario Rossi</responsabile>
</leMera>
![Page 13: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/13.jpg)
Ex.3 – Solu4on (Global elements, nominal types)
![Page 14: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/14.jpg)
Exercise 4: Catena Montuosa From instance to XSD
![Page 15: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/15.jpg)
Ex.4 -‐ From instance to XSD: Catena Montuosa
<catenaMontuosa> <monte> <nome> Monte Bianco </nome>
<regione> Valle d’Aosta </regione> <altezza unitaMisura="metri">4810</altezza>
</monte> <monte> <nome>Gransasso</nome> </monte>
</catenaMontuosa>
![Page 16: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/16.jpg)
Ex.4 – Solu4on 1 (Local elements, anonymous types)
![Page 17: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/17.jpg)
Ex.4 – Solu4on 2 (Global elements, anonymous types)
![Page 18: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/18.jpg)
Exercise 5: XPath
![Page 19: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/19.jpg)
<!ELEMENT recipes (recipe*)> <!ELEMENT recipe (4tle, ingredient+, prepara4on, comment?, nutri4on)> <!ELEMENT 4tle (#PCDATA)> <!ELEMENT ingredient (ingredient*,prepara4on?)> <!ATTLIST ingredient name CDATA #REQUIRED
amount CDATA #IMPLIED unit CDATA #IMPLIED>
<!ELEMENT prepara4on (step+)> <!ELEMENT step (#PCDATA)> <!ELEMENT nutri4on EMPTY> <!ELEMENT comment (#PCDATA)> <!ATTLIST nutri4on
calories CDATA #REQUIRED fat CDATA #REQUIRED carbohydrates CDATA #REQUIRED protein CDATA #REQUIRED alcohol CDATA #IMPLIED>
Ex.5 -‐ DTD
![Page 20: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/20.jpg)
<recipes> <recipe>
<4tle>Beef Parmesan</4tle> <ingredient name="beef" amount="1.5" unit="pound"/> <ingredient name="onion" amount="1"/> <ingredient name="green rings" amount="1"/> <prepara4on> <step>Boil pasta</step> </prepara4on> <comment>… </comment> <nutri4on calories="1167" fat="23"carbohydrates="45" protein="32"/>
</recipe> …. </recipes>
Ex.5 -‐ Instance
![Page 21: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/21.jpg)
1. "The 4tles of all recipes that use olive oil.” 2. "The 4tles of all recipes, returned as strings.” 3. "The 4tles of all recipes with less than 500 calories.” 4. "The amount of sugar needed for Zuppa Inglese.” 5. "The first two steps in preparing Zuppa Inglese.” 6. " The 4tles of all recipes that requires five steps.” 7. "The recipes that have an ingredient in common with Zuppa Inglese.“ 8. "The 4tles of recipes have some compound ingredients."
Ex.5 – XPath Queries
![Page 22: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/22.jpg)
1. "The 4tles of all recipes that use olive oil.”
• //recipe[ingredient/@name="olive oil"]/4tle
Ex.5 -‐ Solu4on
![Page 23: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/23.jpg)
1. "The 4tles of all recipes that use olive oil.”
• //recipe[ingredient/@name="olive oil"]/4tle NO (AMenzione agli ingredien4 nidifica4)
• //recipe[.//ingredient/@name="olive oil"]/4tle
(Vogliamo oMenetere 4toli come ELEMENTI o come testo?) SI • //recipe[.//ingredient/@name="olive oil"]/4tle/text() SI
Ex.5 -‐ Solu4on
![Page 24: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/24.jpg)
1. "The 4tles of all recipes that use olive oil.” 2. "The 4tles of all recipes, returned as strings.”
• //4tle/text() (è possibile specificare il documento tramite fn:doc("recipes.xml"))
Ex.5 -‐ Solu4on
![Page 25: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/25.jpg)
1. "The 4tles of all recipes that use olive oil.” 2. "The 4tles of all recipes, returned as strings.” 3. "The 4tles of all recipes with less than 500 calories.”
• //recipe[//@calories < 500]/4tle
Ex.5 -‐ Solu4on
![Page 26: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/26.jpg)
1. "The 4tles of all recipes that use olive oil.” 2. "The 4tles of all recipes, returned as strings.” 3. "The 4tles of all recipes with less than 500 calories.”
• //recipe[//@calories < 500]/4tle NO (Bisogna specificare l’aMributo di quale elemento -‐ // nelle parantesi torna da root)
• //recipe[nutri4on/@calories < 500]/4tle SI
Ex.5 -‐ Solu4on
![Page 27: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/27.jpg)
1. "The 4tles of all recipes that use olive oil.” 2. "The 4tles of all recipes, returned as strings.” 3. "The 4tles of all recipes with less than 500 calories.” 4. "The amount of sugar needed for Zuppa Inglese.”
• number(//recipe[4tle="Zuppa Inglese"]//ingredient[@name="sugar"]/@amount) (ritorna un numerico dell’ammontare dello zucchero – NON ESEGUE LA SOMMA)
Ex.5 -‐ Solu4on
![Page 28: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/28.jpg)
1. "The 4tles of all recipes that use olive oil.” 2. "The 4tles of all recipes, returned as strings.” 3. "The 4tles of all recipes with less than 500 calories.” 4. "The amount of sugar needed for Zuppa Inglese.” 5. "The first two steps in preparing Zuppa Inglese.”
• //recipe[4tle="Zuppa Inglese"]/prepara4on/step[posi4on()=1 or posi4on()=2]
Ex.5 -‐ Solu4on
![Page 29: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/29.jpg)
1. "The 4tles of all recipes that use olive oil.” 2. "The 4tles of all recipes, returned as strings.” 3. "The 4tles of all recipes with less than 500 calories.” 4. "The amount of sugar needed for Zuppa Inglese.” 5. "The first two steps in preparing Zuppa Inglese.” 6. " The 4tles of all recipes that requires five steps.”
Ex.5 -‐ Solu4on
![Page 30: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/30.jpg)
1. "The 4tles of all recipes that use olive oil.” 2. "The 4tles of all recipes, returned as strings.” 3. "The 4tles of all recipes with less than 500 calories.” 4. "The amount of sugar needed for Zuppa Inglese.” 5. "The first two steps in preparing Zuppa Inglese.” 6. " The 4tles of all recipes that requires five steps.”
• //recipe[.//step[5]]/4tle • //recipe[prepara4on/step[5]]/4tle
Ex.5 -‐ Solu4on
![Page 31: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/31.jpg)
1. "The 4tles of all recipes that use olive oil.” 2. "The 4tles of all recipes, returned as strings.” 3. "The 4tles of all recipes with less than 500 calories.” 4. "The amount of sugar needed for Zuppa Inglese.” 5. "The first two steps in preparing Zuppa Inglese.” 6. " The 4tles of all recipes that requires five steps.” 7. "The recipes that have an ingredient in common with Zuppa Inglese.“
• //recipe[.//ingredient/@name=//recipe[4tle="Zuppa Inglese"]//ingredient/@name]
Ex.5 -‐ Solu4on
![Page 32: Exercises:)XSD,)XPath)atzeni/didattica/BD/20112012/Ex 02 XSD-XPath.pdf · Exercises:! 1xFrom XSD)to)instance) 2xFrom instance)to)XSD) 1xFrom specs)to)XSD))](https://reader030.vdocuments.mx/reader030/viewer/2022040513/5e672313e8eea53286568339/html5/thumbnails/32.jpg)
1. "The 4tles of all recipes that use olive oil.” 2. "The 4tles of all recipes, returned as strings.” 3. "The 4tles of all recipes with less than 500 calories.” 4. "The amount of sugar needed for Zuppa Inglese.” 5. "The first two steps in preparing Zuppa Inglese.” 6. " The 4tles of all recipes that requires five steps.” 7. "The recipes that have an ingredient in common with Zuppa Inglese.“ 8. "The 4tles of recipes have some compound ingredients.”
• //recipe[ingredient/ingredient]/4tle
Ex.5 -‐ Solu4on