View
214
Download
0
Embed Size (px)
Citation preview
Internet Technologies 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
2. Code Generation
3. 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); }
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);
Internet Technologies 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 runBuildfile: 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 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.javagenerated\impl\ItemListImpl.javagenerated\impl\ItemListTypeImpl.javagenerated\impl\ItemTypeImpl.javagenerated\impl\NameImpl.java
Internet Technologies 40
generated\impl\QuantityImpl.javagenerated\Item.javagenerated\ItemList.javagenerated\ItemListType.javagenerated\ItemType.javagenerated\Name.javagenerated\ObjectFactory.javagenerated\Quantity.javagenerated\bgm.sergenerated\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 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 combinedsequence =>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 51
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 52
Occurrence options
• MinOccurs and MaxOccurs
• Default(if not mentioned) MinOccurs = MaxOccurs = “1”
• MinOccurs = “0” element is optional
• MaxOccurs = “unbounded” infinity
Internet Technologies 53
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 54
<element name="addr">
<complexType> <choice> <element ref="co:downtown" /> <element ref="co:uptown" /> </choice> </complexType>
</element>
Downtown or uptown
Internet Technologies 55
<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 56
<element name = "street" type="string" /> <element name = "apt" type="integer" /> </schema>
Internet Technologies 57
<?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 58
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 59
Choices
<element name="addr">
<complexType> <choice maxOccurs = “unbounded"> <element ref="co:downtown" /> <element ref="co:uptown" /> </choice> </complexType>
</element>
Choose 1 and then as manyas you like from the same group
DTD<!ELEMENT addr (downtown |uptown)+ >
Internet Technologies 60
Embedded Groups
<sequence>
<element ref = “doc:title”/>
<choice minOccurs=“0” maxOccurs=“unbounded”>
<element ref=“doc:para”/>
<element ref=“doc:list”/>
</choice>
</sequence>
Required
Choose as many as you like includingnone
DTD (title, (para | list)*)
Internet Technologies 61
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 62
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 63
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.xmlReceived notification of a recoverable error.org.xml.sax.SAXParseException: cvc
complex-type.4: Attribute 'orderDate' must appear on element 'purchaseOrder'.Valid Document is false