640 likes | 655 Views
Learn about XML Schema, its components, alternatives, and major uses. Explore the Purchase Order XSDL example and validation methods with Java.
E N D
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
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
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
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
<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" > 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
Three Major Uses – Same as DTD’s 1. Validation • Code Generation • Communication Internet Technologies
Better than DTD’s • Good support for namespaces • Type Checking • XML Syntax But Harder than DTD’s Internet Technologies
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
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
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
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);
} 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
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
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
<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
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
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
<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
<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
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
<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
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
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
From W3C XML Schema Part 2 DataTypes Internet Technologies
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
<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
<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
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
Code Generation • Run JAXB against the .xsd file • Code generated will present an API allowing us to process that style of document Internet Technologies
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
<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
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
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
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
<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
<!-- 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
More details on XML Schemas 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