1 / 64

XML Schema

XML Schema. The main source for these slides is “The XML Companion” by Bradley Other resources: http://www.w3.org/TR/xmlschema-2/. XML Schema. “XML Schema” is the official name XSDL (XML Schema Definition Language) is the language used to create schema definitions

rozene
Download Presentation

XML Schema

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XML Schema The main source for these slides is “The XML Companion” by Bradley Other resources: http://www.w3.org/TR/xmlschema-2/ Internet Technologies

  2. XML Schema • “XML Schema” is the official name • XSDL (XML Schema Definition Language) is the language • used to create schema definitions • Can be used to more tightly constrain a document instance • Supports namespaces • Permits type derivation Internet Technologies

  3. XSDL Alternatives Include • DTD’s • RELAX • TREX (James Clark - Tree Regular Expressions for XML) • RELAX NG (RELAX and TREX combined to Relax Next Generation) • Schematron (“Rule based” rather than “grammar based” see www.ascc.net/xml/schematron) Based on XSLT and XPath Internet Technologies

  4. XSDL - A Simple Purchase Order <?xml version="1.0" encoding="UTF-8"?> <!-- po.xml --> <purchaseOrder orderDate="07.23.2001" xmlns="http://www.cds-r-us.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.cds-r-us.com po.xsd" > Internet Technologies

  5. <recipient country="USA"> <name>Dennis Scannel</name> <street>175 Perry Lea Side Road</street> <city>Waterbury</city> <state>VT</state> <postalCode>15216</postalCode> </recipient> <order> <cd artist="Brooks Williams" title="Little Lion" /> <cd artist="David Wilcox" title="What you whispered" /> </order> </purchaseOrder> Internet Technologies

  6. Purchase Order XSDL <?xml version="1.0" encoding="utf-8"?> <!-- po.xsd --> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.cds-r-us.com" targetNamespace="http://www.cds-r-us.com" > Internet Technologies

  7. <xs:element name="purchaseOrder"> <xs:complexType> <xs:sequence> <xs:element ref="recipient" /> <xs:element ref="order" /> </xs:sequence> <xs:attribute name="orderDate" type="xs:string" /> </xs:complexType> </xs:element> Internet Technologies

  8. <xs:element name = "recipient"> <xs:complexType> <xs:sequence> <xs:element ref="name" /> <xs:element ref="street" /> <xs:element ref="city" /> <xs:element ref="state" /> <xs:element ref="postalCode" /> </xs:sequence> <xs:attribute name="country" type="xs:string" /> </xs:complexType> </xs:element> Internet Technologies

  9. <xs:element name = "name" type="xs:string" /> <xs:element name = "street" type="xs:string" /> <xs:element name = "city" type="xs:string" /> <xs:element name = "state" type="xs:string" /> <xs:element name = "postalCode" type="xs:short" /> <xs:element name = "order"> <xs:complexType> <xs:sequence> <xs:element ref="cd" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> Internet Technologies

  10. <xs:element name="cd"> <xs:complexType> <xs:attribute name="artist" type="xs:string" /> <xs:attribute name="title" type="xs:string" /> </xs:complexType> </xs:element> </xs:schema> Internet Technologies

  11. Three Major Uses – Same as DTD’s 1. Validation • Code Generation • Communication Internet Technologies

  12. Better than DTD’s • Good support for namespaces • Type Checking • XML Syntax But Harder than DTD’s Internet Technologies

  13. Validate.java // Validate.java using Xerces import java.io.*; import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.XMLReader; import org.xml.sax.InputSource; import org.xml.sax.helpers.XMLReaderFactory; import org.xml.sax.helpers.DefaultHandler; import java.io.*; Internet Technologies

  14. import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.SAXException; import org.xml.sax.InputSource; import org.xml.sax.SAXParseException; Internet Technologies

  15. public class Validate extends DefaultHandler { public static boolean valid = true; public void error(SAXParseException exception) { System.out.println("Received notification of a recoverable error." + exception); valid = false; } public void fatalError(SAXParseException exception) { System.out.println("Received notification of a non-recoverable error."+ exception); valid = false; } public void warning(SAXParseException exception) { System.out.println("Received notification of a warning."+ exception); } Internet Technologies

  16. public static void main (String argv []) { if (argv.length != 1) { System.err.println ("Usage: java Validate filename.xml"); System.exit (1); } try { // get a parser XMLReader reader = XMLReaderFactory.createXMLReader( "org.apache.xerces.parsers.SAXParser"); // request validation reader.setFeature("http://xml.org/sax/features/validation",true); reader.setFeature( "http://apache.org/xml/features/validation/schema",true); reader.setErrorHandler(new Validate()); // associate an InputSource object with the file name InputSource inputSource = new InputSource(argv[0]); // go ahead and parse reader.parse(inputSource);

  17. } catch(org.xml.sax.SAXException e) { System.out.println("Error in parsing " + e); valid = false; } catch(java.io.IOException e) { System.out.println("Error in I/O " + e); System.exit(0); } System.out.println("Valid Document is " + valid); } } Internet Technologies

  18. XML Document <?xml version="1.0" encoding="utf-8"?> <itemList xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation="itemList.xsd"> <item> <name>pen</name> <quantity>5</quantity> </item> <item> <name>eraser</name> <quantity>7</quantity> </item> <item> <name>stapler</name> <quantity>2</quantity> </item> </itemList> Internet Technologies

  19. XSDL Grammar itemList.xsd <?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'> <xsd:element name="itemList"> <xsd:complexType> <xsd:sequence> <xsd:element ref="item" minOccurs="0" maxOccurs="3"/> </xsd:sequence> </xsd:complexType> </xsd:element> Internet Technologies

  20. <xsd:element name="item"> <xsd:complexType> <xsd:sequence> <xsd:element ref="name"/> <xsd:element ref="quantity"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="name" type="xsd:string"/> <xsd:element name="quantity" type="xsd:short"/> </xsd:schema> Internet Technologies

  21. D:..95-733\examples\XSDL\testing>ant run Buildfile: build.xml run: Running Validate.java on itemList-xsd.xml Valid Document is true Internet Technologies

  22. Purchase again (with Prefixes) <?xml version="1.0" encoding="UTF-8"?> <!-- po.xml --> <myns:purchaseOrder orderDate="07.23.2001" xmlns:myns="http://www.cds-r-us.com" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.cds-r-us.com po.xsd" > Internet Technologies

  23. <myns:recipient country="USA"> <myns:name>Dennis Scannel</myns:name> <myns:street>175 Perry Lea Side Road</myns:street> <myns:city>Waterbury</myns:city> <myns:state>VT</myns:state> <myns:postalCode>05675A</myns:postalCode> </myns:recipient> Note that there is a problem with this document. Internet Technologies

  24. <myns:order> <myns:cd artist="Brooks Williams" title="Little Lion" /> <myns:cd artist="David Wilcox" title="What you whispered" /> </myns:order> </myns:purchaseOrder> Internet Technologies

  25. XSDL Grammar po.xsd <?xml version="1.0" encoding="utf-8"?> <!-- po.xsd --> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.cds-r-us.com" targetNamespace="http://www.cds-r-us.com" > <xs:element name="purchaseOrder"> <xs:complexType> <xs:sequence> <xs:element ref="recipient" /> <xs:element ref="order" /> </xs:sequence> <xs:attribute name="orderDate" type="xs:string" /> </xs:complexType> </xs:element> Internet Technologies

  26. <xs:element name = "recipient"> <xs:complexType> <xs:sequence> <xs:element ref="name" /> <xs:element ref="street" /> <xs:element ref="city" /> <xs:element ref="state" /> <xs:element ref="postalCode" /> </xs:sequence> <xs:attribute name="country" type="xs:string" /> </xs:complexType> </xs:element> Internet Technologies

  27. <xs:element name = "name" type="xs:string" /> <xs:element name = "street" type="xs:string" /> <xs:element name = "city" type="xs:string" /> <xs:element name = "state" type="xs:string" /> <xs:element name = "postalCode" type="xs:short" /> <xs:element name = "order"> <xs:complexType> <xs:sequence> <xs:element ref="cd" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> Internet Technologies

  28. <xs:element name="cd"> <xs:complexType> <xs:attribute name="artist" type="xs:string" /> <xs:attribute name="title" type="xs:string" /> </xs:complexType> </xs:element> </xs:schema> Internet Technologies

  29. Running Validate D:..\examples\XSDL\testing>ant run Buildfile: build.xml run: Running Validate.java on po.xml Received notification of a recoverable error.org.xml.sax.SAXParseException: cvc-datatype-valid.1.2.1: '05675A' is not a valid 'integer' value. Received notification of a recoverable error.org.xml.sax.SAXParseException: cvc-type.3.1.3: The value '05675A' of element 'myns:postalCode' is not valid. Valid Document is false Internet Technologies

  30. Fix the error and run again D:\..\XSDL\testing>ant run Buildfile: build.xml run: Running Validate.java on po.xml Valid Document is true Internet Technologies

  31. From W3C XML Schema Part 2 DataTypes Internet Technologies

  32. Introduce a Namespace Error <?xml version="1.0" encoding="UTF-8"?> <!-- po.xml --> <myns:purchaseOrder orderDate="07.23.2001" xmlns:myns="http://www.cds-r-us.edu" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.cds-r-us.com po.xsd" > Internet Technologies

  33. <myns:recipient country="USA"> <myns:name>Dennis Scannel</myns:name> <myns:street> 175 Perry Lea Side Road </myns:street> <myns:city>Waterbury</myns:city> <myns:state>VT</myns:state> <myns:postalCode>05675</myns:postalCode> </myns:recipient> Internet Technologies

  34. <myns:order> <myns:cd artist="Brooks Williams" title="Little Lion" /> <myns:cd artist="David Wilcox" title="What you whispered" /> </myns:order> </myns:purchaseOrder> Internet Technologies

  35. And run validate run: Running Validate.java on po.xml Received notification of a recoverable error.org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'myns:purchaseOrder'. Valid Document is false Internet Technologies

  36. Code Generation • Run JAXB against the .xsd file • Code generated will present an API allowing us to process that style of document Internet Technologies

  37. itemList.xsd again <?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'> <xsd:element name="itemList"> <xsd:complexType> <xsd:sequence> <xsd:element ref="item" minOccurs="0" maxOccurs="3"/> </xsd:sequence> </xsd:complexType> </xsd:element> Internet Technologies

  38. <xsd:element name="item"> <xsd:complexType> <xsd:sequence> <xsd:element ref="name"/> <xsd:element ref="quantity"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="name" type="xsd:string"/> <xsd:element name="quantity" type="xsd:short"/> </xsd:schema> Internet Technologies

  39. Run xjc D:..XSDL\testing>xjc itemList.xsd D:\McCarthy\www\95-733\examples\XSDL\testing>java -jar D:\jwsdp-1.1\jaxb-1.0\lib \jaxb-xjc.jar itemList.xsd parsing a schema... compiling a schema... generated\impl\ItemImpl.java generated\impl\ItemListImpl.java generated\impl\ItemListTypeImpl.java generated\impl\ItemTypeImpl.java generated\impl\NameImpl.java Internet Technologies

  40. generated\impl\QuantityImpl.java generated\Item.java generated\ItemList.java generated\ItemListType.java generated\ItemType.java generated\Name.java generated\ObjectFactory.java generated\Quantity.java generated\bgm.ser generated\jaxb.properties Write Java Code That uses NEW the api Internet Technologies

  41. The Ant build script used for these examples is also XML <?xml version="1.0"?> <project basedir="." default="compile"> <path id="classpath"> <fileset dir="D:/jwsdp-1.1/saaj-1.1.1/lib" includes="*.jar"/> <fileset dir="D:/jwsdp-1.1/jaxb-1.0/lib" includes="*.jar"/> <fileset dir="d:/jwsdp-1.1/common/lib" includes="*.jar"/> Internet Technologies

  42. <fileset dir="D:/jwsdp-1.1/jaxm-1.1.1/lib" includes="*.jar"/> <fileset dir="D:/jwsdp-1.1/bin" includes="*.jar" /> <fileset dir="D:/jwsdp-1.1/jaxp-1.2.2/lib" includes="*.jar"/> <fileset dir="D:/jwsdp-1.1/jaxp-1.2.2/lib/endorsed" includes="*.jar"/> <fileset dir="D:/jwsdp-1.1/jwsdp-shared/lib" includes="*.jar"/> <fileset dir="D:/jwsdp-1.1/jaxr-1.0_03/lib" includes="*.jar"/> <fileset dir="D:/jwsdp-1.1/jakarta-ant-1.5.1/lib" includes="*.jar"/> <fileset dir="D:/j2sdk1.4.1_01/lib" includes="*.jar"/> <pathelement location="."/> </path> Internet Technologies

  43. <!-- compile Java source files --> <target name="compile"> <!-- compile all of the java sources --> <echo message="Compiling the java source files..."/> <javac srcdir="." destdir="." debug="on"> <classpath refid="classpath" /> </javac> </target> <target name="run"> <echo message="Running Validate.java on po.xml"/> <java classname="Validate" fork="fasle"> <arg value="po.xml"/> <classpath refid="classpath" /> </java> </target> </project> Internet Technologies

  44. More details on XML Schemas Internet Technologies

  45. Comments in XSDL <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.cds-r-us.com" targetNamespace="http://www.cds-r-us.com" > <xs:annotation> <xs:documentation>This is a comment. </xs:documentation> </xs:annotation> : : Internet Technologies

  46. Element Definitions • The Element element is used to define an element • The Element element may be empty <xs:element name = "name" type="xs:string" /> • Or, may contain content <xs:element name="cd"> <xs:complexType> <xs:attribute name="artist" type="xs:string" /> <xs:attribute name="title" type="xs:string" /> </xs:complexType> </xs:element> Internet Technologies

  47. Element Definitions • The Element element may be empty and contain no other attributes <xs:element name = "purchaseOrder"/> • This purchaseOrder element may contain anything (more elements and text) • DTD <!ELEMENT purchaseOrder ANY> Internet Technologies

  48. Simple Content • An element may be defined to hold only a number, word or text <xs:element name = "city" type="xs:string" /> • DTD <!ELEMENT city (#PCDATA)> Internet Technologies

  49. Complex Content • The element may contain child elements or attributes <xs:element name="purchaseOrder"> <xs:complexType> <xs:sequence> <xs:element ref="recipient" /> <xs:element ref="order" /> </xs:sequence> <xs:attribute name="orderDate" type="xs:string" /> </xs:complexType> </xs:element> An indicator on how these elements are to be combined sequence =>predefined order Internet Technologies

  50. Place Holder Element Definitions <element name=“pageBreak”> <complexType></complexType> </element> • No content is permitted DTD <!ELEMENT pageBreak EMPTY> <!ATTLIST pageBreak> Internet Technologies

More Related