1 / 36

Introduction to XML Schemas

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

Download Presentation

Introduction to XML Schemas

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. Introduction to XML Schemas • 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 Schemas • “XML Schema” is the official name • XSDL (XML Schema Definition Language) is the language • used to create schema definitions • Can be used to tightly constrain a document instance • Supports namespaces • Permits type derivation Internet Technologies

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

  4. Better than DTD’s • Good support for namespaces (namespaces came after DTD’s) • Type Checking (DTD’s use #PCDATA) • XML Syntax (DTD’s are not XML) • XML tools, like editors, will work with XSDL Internet Technologies

  5. // Validate.java using Xerces // Program written by Kunal Kaviraj 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; Internet Technologies

  6. public class Validate implements ErrorHandler { public static boolean valid = true; public static void main (String argv []) throws SAXException, IOException { if (argv.length != 1) { System.err.println ("Usage: java Validate filename.xml"); System.exit (1); } // get a parser Internet Technologies

  7. 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); System.out.println("Valid Document is " + valid); } Internet Technologies

  8. public void warning(SAXParseException exception) { System.out.println("Validate:Warning" + exception); valid = false; } public void error(SAXParseException exception) { System.out.println("Validate:Error" + exception); valid = false; } public void fatalError(SAXParseException exception) { System.out.println("Validate:fatalError" + exception); valid = false; } } Internet Technologies

  9. 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

  10. <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

  11. 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" > XSDL Standard namespace Target namespace Internet Technologies

  12. <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

  13. <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

  14. <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

  15. <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

  16. D:..\95-733\examples\XSDL\testing>java Validate po.xml Valid Document is true Internet Technologies

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

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

  23. Namespace Issues • All element definitions belong to a target document-type namespace • If a prefix is used for schema elements (as we have done above) then we need to specify that prefix on datatypes to distinguish those defined in XSDL from those the author may define. <xs:attribute name="orderDate" type="xs:string" /> Internet Technologies

  24. Occurrence options • MinOccurs and MaxOccurs • Default(if not mentioned) MinOccurs = MaxOccurs = “1” • MinOccurs = “0” element is optional • MaxOccurs = “unbounded” infinity Internet Technologies

  25. Choices And a New Example <?xml version="1.0" encoding="utf-8"?> <!-- addr.xsd --> <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:co="http://www.myCompany.com" targetNamespace="http://www.myCompany.com" > <annotation> <documentation>This is the company address XSDL document. </documentation> </annotation> comment Internet Technologies

  26. <element name="addr"> <complexType> <choice> <element ref="co:downtown" /> <element ref="co:uptown" /> </choice> </complexType> </element> Downtown or uptown Internet Technologies

  27. <element name = "downtown"> <complexType> <sequence> <element ref="co:street" /> </sequence> </complexType> </element> <element name = "uptown"> <complexType> <sequence> <element ref="co:street" /> <element ref="co:apt" /> </sequence> </complexType> </element> Internet Technologies

  28. <element name = "street" type="string" /> <element name = "apt" type="integer" /> </schema> Internet Technologies

  29. <?xml version="1.0" encoding="UTF-8"?> <!– addr.xml --> <addr xmlns="http://www.myCompany.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.myCompany.com address.xsd" > <downtown> <street>Fifth Avenue</street> </downtown> </addr> java Validate addr.xml Valid document is true Internet Technologies

  30. Choices <element name="addr"> <complexType> <choice minOccurs = "0"> <element ref="co:downtown" /> <element ref="co:uptown" /> </choice> </complexType> </element> Choose 1 or none DTD <!ELEMENT addr (downtown | uptown)? > Internet Technologies

  31. Choices Choose 1 and then as many as you like from the same group <element name="addr"> <complexType> <choice maxOccurs = “unbounded"> <element ref="co:downtown" /> <element ref="co:uptown" /> </choice> </complexType> </element> DTD <!ELEMENT addr (downtown | uptown)+ > Internet Technologies

  32. Embedded Groups Required <sequence> <element ref = “doc:title”/> <choice minOccurs=“0” maxOccurs=“unbounded”> <element ref=“doc:para”/> <element ref=“doc:list”/> </choice> </sequence> Choose as many as you like including none DTD (title, (para | list)*) Internet Technologies

  33. Mixed Content <element name=“para”> <complexType mixed=“true”> <choice minOccurs=“0” maxOccurs=“unbounded”? <element ref=“doc:emph” /> <element ref=“doc:name” /> </choice> </complexType> </element> DTD <!ELEMENT para (#PCDATA | emph | name)*> Text is always optional. Internet Technologies

  34. Attributes • May never contain element tags or sub-elements • So, attribute type values will always be simple types • By default, attributes are optional <xs:attribute name="orderDate" type="xs:string" /> Internet Technologies

  35. Attributes • Can be required, prohibited, optional <xs:attribute name="orderDate" type="xs:string“ use=“required” /> After removing the attribute …. D:..\95-733\examples\XSDL\testing>java Validate po.xml Received notification of a recoverable error.org.xml.sax.SAXParseException: cvc complex-ty pe.4: Attribute 'orderDate' must appear on element 'purchaseOrder'. Valid Document is false Internet Technologies

  36. Attribute Types <xs:attribute name="orderDate" type="xs:date“ use=“required” /> Does not validate <purchaseOrder orderDate="07.23.2001" Validates <purchaseOrder orderDate="1955-12-17" Internet Technologies

More Related