360 likes | 395 Views
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
E N D
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
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
Three Major Uses – Same as DTD’s 1. Validation • Code Generation • Communication Internet Technologies
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
// 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
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
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
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
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
<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
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
<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
<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
<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
<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
D:..\95-733\examples\XSDL\testing>java Validate po.xml Valid Document is true Internet Technologies
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
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
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
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
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
Place Holder Element Definitions <element name=“pageBreak”> <complexType></complexType> </element> • No content is permitted DTD <!ELEMENT pageBreak EMPTY> <!ATTLIST pageBreak> Internet Technologies
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
Occurrence options • MinOccurs and MaxOccurs • Default(if not mentioned) MinOccurs = MaxOccurs = “1” • MinOccurs = “0” element is optional • MaxOccurs = “unbounded” infinity Internet Technologies
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
<element name="addr"> <complexType> <choice> <element ref="co:downtown" /> <element ref="co:uptown" /> </choice> </complexType> </element> Downtown or uptown Internet Technologies
<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
<element name = "street" type="string" /> <element name = "apt" type="integer" /> </schema> Internet Technologies
<?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
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
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
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
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
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
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
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