Internet Technologies1 More XML Schema The main source for these slides is “The XML Companion”...

Post on 21-Dec-2015

217 views 4 download

transcript

Internet Technologies 1

More 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

Three Major Uses – Same as DTD’s

1. Validation

2. Code Generation

3. Communication

Internet Technologies 3

Better than DTD’s

• Good support for namespaces (namespaces came after DTD’s)

• Type Checking (DTD’s use #PCDATA)

See next slide

• XML Syntax (DTD’s are not XML)

• XML tools, like editors, will work with XSDL

Internet Technologies 4

From W3CXML SchemaPart 2DataTypes

Internet Technologies 5

// Validate.java using Xerces// Program written by Kunal Kavirajimport 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

namespaceTarget 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 combinedsequence =>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

<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 32

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

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"