Top Banner
SDPL 2005 Notes 2.5: XML Schemas 1 2.5 XML Schemas 2.5 XML Schemas Short introduction to XML Schema Short introduction to XML Schema W3C Recommendation, W3C Recommendation, 1 1 st st Ed. May, 2001; 2 Ed. May, 2001; 2 nd nd Ed. Oct, 2004: Ed. Oct, 2004: » XML Schema Part 0: Primer (readable non- XML Schema Part 0: Primer (readable non- normative introduction; Recommended) normative introduction; Recommended) » XML Schema Part 1: Structures XML Schema Part 1: Structures » XML Schema Part 2: Datatypes XML Schema Part 2: Datatypes Also started (but didn't lead Also started (but didn't lead anywhere?): anywhere?): » XML Schema: Formal Description, W3C Working XML Schema: Formal Description, W3C Working Draft, September 2001 Draft, September 2001
22

SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

Jan 02, 2016

Download

Documents

Jasper Phillips
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 1

2.5 XML Schemas2.5 XML Schemas

Short introduction to XML SchemaShort introduction to XML Schema– W3C Recommendation,W3C Recommendation,

11stst Ed. May, 2001; 2 Ed. May, 2001; 2ndnd Ed. Oct, 2004: Ed. Oct, 2004:» XML Schema Part 0: Primer (readable non-XML Schema Part 0: Primer (readable non-

normative introduction; Recommended)normative introduction; Recommended)

» XML Schema Part 1: StructuresXML Schema Part 1: Structures

» XML Schema Part 2: DatatypesXML Schema Part 2: Datatypes

– Also started (but didn't lead anywhere?):Also started (but didn't lead anywhere?):» XML Schema: Formal Description, W3C XML Schema: Formal Description, W3C

Working Draft, September 2001Working Draft, September 2001

Page 2: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 2

Schema terminologySchema terminology

Schema Schema ((kaaviokaavio): a formal description for the ): a formal description for the structure and allowed contents of data structure and allowed contents of data (esp. in databases)(esp. in databases)

““XML Schema” is often used for each of … XML Schema” is often used for each of … 1.1. XML SchemaXML Schema, the W3C Rec. that defines … , the W3C Rec. that defines … 2.2. XML Schema Definition LanguageXML Schema Definition Language ( (XSDLXSDL), ),

an XML-based markup language for an XML-based markup language for expressing ... expressing ... 3.3. schema documentsschema documents, each of which describes a , each of which describes a schema (schema ( DTD) for a set of XML document instances DTD) for a set of XML document instances

(This may cause some confusion!)(This may cause some confusion!)

Page 3: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 3

Advantages of XSDL (1)Advantages of XSDL (1)

XML syntaxXML syntax– schema documents easier to manipulate by schema documents easier to manipulate by

programs (than the special DTD syntax)programs (than the special DTD syntax) Compatibility with namespacesCompatibility with namespaces

– can validate documents using declarations from can validate documents using declarations from multiple sourcesmultiple sources

Content datatypesContent datatypes– 44 built-in datatypes (including primitive Java 44 built-in datatypes (including primitive Java

datatypes, datatypes of SQL, and XML attribute datatypes, datatypes of SQL, and XML attribute types)types)

– mechanisms to derive user-defined datatypesmechanisms to derive user-defined datatypes

Page 4: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 4

XSDL built-in XSDL built-in types types (Part 2, Chap. 3)(Part 2, Chap. 3)

NB: all simple values in NB: all simple values in documents documents stringsstrings

**CDATACDATA

**

**

**

**

**

**

**

**

*: XML attribute *: XML attribute typestypes

Page 5: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 5

Advantages of XSDL (2)Advantages of XSDL (2)

Element names and Element names and content typescontent types independent; Compare with independent; Compare with – DTDs: 1-to-1 correspondence btw. element type DTDs: 1-to-1 correspondence btw. element type

names and their content models names and their content models – CFGs: 1-to-1 correspondence btw. nonterminals CFGs: 1-to-1 correspondence btw. nonterminals

and their productionsand their productions For example, could define For example, could define titlestitles

– of people as “Mr.”/”Mrs.”/”Ms.”, andof people as “Mr.”/”Mrs.”/”Ms.”, and– of chapters as stringsof chapters as strings

Page 6: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 6

Advantages of XSDL (3)Advantages of XSDL (3)

Ability to specify uniqueness and keys Ability to specify uniqueness and keys within selected parts of the documentwithin selected parts of the document– for example, that for example, that titletitless of chapters should be of chapters should be

unique; or key attributes of relationsunique; or key attributes of relations– uses XPathuses XPath

Support for schema documentation Support for schema documentation – element element annotationannotation with sub-elements with sub-elements

documentation documentation (for human readers) and(for human readers) andappInfo appInfo (for applications)(for applications)

Page 7: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 7

Disadvantages of XSDLDisadvantages of XSDL

Complexity of XSDL (esp. of Rec. Part 1!) vs. added Complexity of XSDL (esp. of Rec. Part 1!) vs. added power power – > a long learning curve> a long learning curve– > possible rejection by users> possible rejection by users

Possible immaturity of implementations (?)Possible immaturity of implementations (?)– W3C XML Schema Web site mentions ~40 tools or W3C XML Schema Web site mentions ~40 tools or

processors (processors (http://www.w3.org/XML/Schema#Tools, March http://www.w3.org/XML/Schema#Tools, March 2004)2004)

– Open-source Apache XML parsers (Xerces C++ 2.0 and Open-source Apache XML parsers (Xerces C++ 2.0 and Xerces 2 Java) claim full XML Schema supportXerces 2 Java) claim full XML Schema support

Alternative schema languages have been Alternative schema languages have been suggested, too (See later)suggested, too (See later)

Page 8: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 8

XSDL through ExampleXSDL through Example

– Next: walk-through of an XML schema exampleNext: walk-through of an XML schema example– from Chapter 2 of the XML Schema Primerfrom Chapter 2 of the XML Schema Primer

– Consider modelling purchase orders like below:Consider modelling purchase orders like below:

<purchaseOrder orderDate="1999-10-20"><purchaseOrder orderDate="1999-10-20"> <shipTo country="US"> <shipTo country="US"> <name>Alice Smith</name> <name>Alice Smith</name> <street>123 Maple Street</street> <street>123 Maple Street</street> <city>Mill Valley</city> <city>Mill Valley</city> <state>CA</state> <state>CA</state> <zip>90952</zip> <zip>90952</zip> </shipTo> </shipTo>

Page 9: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 9

purchaseOrder purchaseOrder instance instance continuescontinues

<billTo country="US"><billTo country="US"> <name>Robert Smith</name> <name>Robert Smith</name> <street>8 Oak Avenue</street> <street>8 Oak Avenue</street> <city>Old Town</city> <city>Old Town</city> <state>PA</state> <state>PA</state> <zip>95819</zip> </billTo> <zip>95819</zip> </billTo> <comment>Hurry, my lawn is wild!</comment> <comment>Hurry, my lawn is wild!</comment>

<items><item partNum="872-AA"><items><item partNum="872-AA"> <productName>Lawnmower</productName><productName>Lawnmower</productName> <quantity>1</quantity><quantity>1</quantity> <USPrice>148.95</USPrice><USPrice>148.95</USPrice> <comment>Only if electric</comment><comment>Only if electric</comment> </item></item>

Page 10: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 10

End of the example instanceEnd of the example instance

<item partNum="926-AA"><item partNum="926-AA"> <productName>Baby Phone</productName><productName>Baby Phone</productName> <quantity>1</quantity><quantity>1</quantity> <USPrice>39.98</USPrice><USPrice>39.98</USPrice> <shipDate>1999-05-21</shipDate><shipDate>1999-05-21</shipDate> </item></item> </items></items></purchaseOrder></purchaseOrder>

Next: A schema for such purchase ordersNext: A schema for such purchase orders

Page 11: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 11

The Purchase Order Schema (1/5)The Purchase Order Schema (1/5)

<xs:schema <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="purchaseOrder" type="POrdType"/><xs:element name="purchaseOrder" type="POrdType"/>

<xs:element name="comment" type="xs:string"/><xs:element name="comment" type="xs:string"/>

<xs:complexType name="POrdType"> <xs:complexType name="POrdType"> <xs:sequence> <xs:sequence>

<xs:element name="shipTo" type="USAddr"/> <xs:element name="shipTo" type="USAddr"/> <xs:element name="billTo" type="USAddr"/> <xs:element name="billTo" type="USAddr"/> <xs:element ref="comment" minOccurs="0"/> <xs:element ref="comment" minOccurs="0"/> <xs:element name="items" type="Items"/> <xs:element name="items" type="Items"/>

</xs:sequence> </xs:sequence> <xs:attribute name="ordDate" type="xs:date"/> <xs:attribute name="ordDate" type="xs:date"/> </xs:complexType></xs:complexType>

Page 12: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 12

The Purchase Order Schema (2/5)The Purchase Order Schema (2/5)

<xs:complexType name="USAddr"> <xs:complexType name="USAddr"> <xs:sequence> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="name" type="xs:string"/> <xs:element name="street" <xs:element name="street"

type="xs:string"/> type="xs:string"/> <xs:element name="city" <xs:element name="city" type="xs:string"/> type="xs:string"/> <xs:element name="state" <xs:element name="state" type="xs:string"/> type="xs:string"/> <xs:element name="zip" <xs:element name="zip" type="xs:decimal"/> type="xs:decimal"/> </xs:sequence> </xs:sequence>

<xs:attribute name="country" <xs:attribute name="country" type="xs:NMTOKEN" fixed="US"/> type="xs:NMTOKEN" fixed="US"/>

</xs:complexType> </xs:complexType>

Page 13: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 13

The Purchase Order Schema (3/5)The Purchase Order Schema (3/5)

<xs:complexType name="Items"> <xs:complexType name="Items"> <xs:sequence> <xs:sequence> <xs:element name="item" <xs:element name="item"

minOccurs="0" maxOccurs="unbounded"> minOccurs="0" maxOccurs="unbounded"> <xs:complexType> <xs:complexType> <xs:sequence> <xs:sequence>

<xs:element name="productName" <xs:element name="productName" type="xs:string"/> type="xs:string"/>

<xs:element name="quantity"> <xs:element name="quantity"> <xs:simpleType> <xs:simpleType>

<xs:restriction <xs:restriction base="xs:positiveInteger"> base="xs:positiveInteger">

<xs:maxExclusive value="100"/> <xs:maxExclusive value="100"/> </xs:restriction> </xs:restriction>

</xs:simpleType> </xs:simpleType> </xs:element> </xs:element>

anonymous type for anonymous type for itemitem

anon. type anon. type for for

quantityquantity

Page 14: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 14

The Purchase Order Schema (4/5)The Purchase Order Schema (4/5)

<xs:element name="USPrice" <xs:element name="USPrice" type="xs:decimal"/> type="xs:decimal"/>

<xs:element ref="comment" <xs:element ref="comment" minOccurs="0"/> minOccurs="0"/>

<xs:element name="shipDate" <xs:element name="shipDate" type="xs:date" type="xs:date"

minOccurs="0"/> minOccurs="0"/> </xs:sequence> </xs:sequence> <xs:attribute name="partNum" type="SKU" <xs:attribute name="partNum" type="SKU"

use="required"/> use="required"/> </xs:complexType> </xs:complexType> </xs:element> <!-- item --> </xs:element> <!-- item --> </xs:sequence> </xs:sequence>

</xs:complexType> <!-- Items --></xs:complexType> <!-- Items -->

Page 15: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 15

The Purchase Order Schema (5/5)The Purchase Order Schema (5/5)

<!-- Type for Stock Keeping Units, <!-- Type for Stock Keeping Units, (codes for identifying products): --> (codes for identifying products): -->

<xs:simpleType name="SKU"> <xs:simpleType name="SKU"> <xs:restriction base="xs:string"><xs:restriction base="xs:string"><!-- defined by a regular expr: --> <!-- defined by a regular expr: --> <xs:pattern value="\d{3}-[A-Z]{2}" /> <xs:pattern value="\d{3}-[A-Z]{2}" />

<!-- 3 digits, hyphen, 2 letters --> <!-- 3 digits, hyphen, 2 letters --> </xs:restriction> </xs:restriction>

</xs:simpleType> </xs:simpleType>

</xs:schema> </xs:schema>

Page 16: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 16

XSDL Content Models XSDL Content Models

Element content of Element content of complexType complexType can be can be regulated using regulated using – group elements group elements sequencesequence, , choice choice and and allall,,

and and

– occurrence constraint attributes occurrence constraint attributes minOccurs minOccurs and and maxOccursmaxOccurs

Elements Elements sequencesequence and and choice choice correspond to catenation and alternation correspond to catenation and alternation ( | ) in ( | ) in regularregular expressions expressions

Page 17: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 17

XSDL occurrence constraints XSDL occurrence constraints

– optionality (E?) can be expressed by optionality (E?) can be expressed by minOccurs=’0’minOccurs=’0’

– iteration (Eiteration (E**) can be expressed by ) can be expressed by minOccurs=’0’minOccurs=’0’andand maxOccurs=’unbounded’maxOccurs=’unbounded’

– Exactly five occurrences of element A:Exactly five occurrences of element A:<xs:element name=”A” <xs:element name=”A” minOccurs=’5’ minOccurs=’5’

maxOccurs=’5’ />maxOccurs=’5’ />

– 10 to 900 occurrences of element A:10 to 900 occurrences of element A:<xs:element name=”A” <xs:element name=”A” minOccurs=’10’ minOccurs=’10’

maxOccurs=’900’ />maxOccurs=’900’ />

default of both is default of both is 11

Page 18: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 18

Regular expression vs an XSDL content model Regular expression vs an XSDL content model

AA+ + | B (C D)| B (C D)** could be expressed by could be expressed by <xs:choice> <xs:choice> <xs:element ref=”A” <xs:element ref=”A”

maxOccurs=’unbounded’ />maxOccurs=’unbounded’ /> <xs:sequence> <xs:sequence>

<xs:element ref=”B” /><xs:element ref=”B” /> <xs:sequence minOccurs=’0’ <xs:sequence minOccurs=’0’

maxOccurs=’unbounded’ > maxOccurs=’unbounded’ > <xs:element ref=”C” /> <xs:element ref=”C” /> <xs:element ref=”D” /> <xs:element ref=”D” />

</xs:sequence> </xs:sequence> </xs:sequence> </xs:sequence> </xs:choice> </xs:choice>

Page 19: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 19

Unordered content: the Unordered content: the allall group group

XSDL XSDL all all group is a restricted version of group is a restricted version of the &-connector in SGMLthe &-connector in SGML– E1 & … & En allows sequences corresponding E1 & … & En allows sequences corresponding

to any permutation of E1, …, Ento any permutation of E1, …, En

XSDL restrictions to XSDL restrictions to all:all:– cannot be combined with other content model cannot be combined with other content model

groupsgroups» can have only (non-iterative) elements as childrencan have only (non-iterative) elements as children

– can be optional (can be optional (minOccurs=0minOccurs=0) but not ) but not repeated (repeated (maxOccurs=1maxOccurs=1))

Page 20: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 20

The The allall group: An example group: An example

For example For example <xs:<xs:allall> > <xs:element name=”A” /> <xs:element name=”A” /> <xs:element name=”<xs:element name=”BB” /> ” /> <xs:element name=”<xs:element name=”CC” ” minminOccurs=’Occurs=’00’ />’ /></xs:</xs:allall>>

accepts the following element sequences:accepts the following element sequences:

A B C; A C B; B A C; B C A; C A B; C B A;A B C; A C B; B A C; B C A; C A B; C B A;A B; and B A;A B; and B A;

Page 21: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 21

XML Schema: SummaryXML Schema: Summary

XSDL: an XML-based grammar XSDL: an XML-based grammar formalismformalism– W3C RecommendationW3C Recommendation– Alternative to the DTD formalismAlternative to the DTD formalism– more powerful than DTDs (esp. content more powerful than DTDs (esp. content

and attribute datatypes)and attribute datatypes) Rather well accepted in XML industryRather well accepted in XML industry

– e.g., to describe messages btw e.g., to describe messages btw Web servicesWeb services; ; WSDL (Web Services Description Language, Vers. WSDL (Web Services Description Language, Vers. 2.0, W3C Working Draft, 8/04) requires XSDL 2.0, W3C Working Draft, 8/04) requires XSDL support of processorssupport of processors

Page 22: SDPL 2005Notes 2.5: XML Schemas1 2.5 XML Schemas n Short introduction to XML Schema –W3C Recommendation, 1 st Ed. May, 2001; 2 nd Ed. Oct, 2004: »XML Schema.

SDPL 2005 Notes 2.5: XML Schemas 22

XML Schema: Future?XML Schema: Future?

XSDL has also been criticizedXSDL has also been criticized– for overwhelming complexity vs. for overwhelming complexity vs.

relatively little added power relatively little added power – other XML-based schema languages other XML-based schema languages

proposed and implemented, too proposed and implemented, too – For example, RELAX NGFor example, RELAX NG

» Draft international ISO/IEC standard Draft international ISO/IEC standard » Simpler and with a rigorous formal base, and Simpler and with a rigorous formal base, and

more powerful at certain places (e.g. more powerful at certain places (e.g. dependencies of attributes, unordered dependencies of attributes, unordered content)content)