introduction to xml schema winter 2003. sources xml schema part 1: structures w3c recommendation 2...

42
Introduction to XML Schema Winter 2003

Post on 21-Dec-2015

234 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Introduction to XML Schema

Winter 2003

Page 2: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Sources

• XML Schema Part 1: Structures W3C Recommendation 2 May 2001, http://www.w3.org/TR/xmlschema-1/

• XML Schema Part 0 Primer• Definitive XML Schema (Priscilla Walmsley)• Database and Transaction Processing – An

application-oriented Approach (Lewis, Bernstein and Kifer)

Page 3: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

XML Schema

• Decouple element types and their names

• Many simple types (xsd:int etc.)• Namespace awareness• Derived types by extension and

restriction• Integrity constraints: key, keyref• “Deterministic”

Page 4: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

XML Schema Example File<?xml version="1.0"?><purchaseOrder orderDate="1999-10-20"> <shipTo country="US"> <name>Alice Smith</name> <street>123 Maple Street</street> <city>Mill Valley</city> <state>CA</state> <zip>90952</zip> </shipTo> <billTo country="US"> <name>Robert Smith</name> <street>8 Oak Avenue</street> <city>Old Town</city> <state>PA</state> <zip>95819</zip> </billTo>

Page 5: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

XML Schema Example File (Cont.)

<comment>Hurry, my lawn is going wild!</comment> <items> <item partNum="872-AA"> <productName>Lawnmower</productName> <quantity>1</quantity> <USPrice>148.95</USPrice> <comment>Confirm this is

electric</comment> </item> <item partNum="926-AA"> <productName>Baby

Monitor</productName> <quantity>1</quantity> <USPrice>39.98</USPrice> <shipDate>1999-05-21</shipDate> </item> </items></purchaseOrder>

Page 6: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

XML Schema for Example File<xsd:schema

xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:annotation> <xsd:documentation xml:lang="en"> Purchase order schema for Example.com. Copyright 2000 Example.com. All rights

reserved. </xsd:documentation> </xsd:annotation>  <xsd:element name="purchaseOrder"

type="PurchaseOrderType"/>  <xsd:element name="comment" type="xsd:string"/> 

Page 7: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

XML Schema for Example File<xsd:complexType name="PurchaseOrderType"> <xsd:sequence> <xsd:element name="shipTo" type="USAddress"/> <xsd:element name="billTo" type="USAddress"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="items" type="Items"/> </xsd:sequence> <xsd:attribute name="orderDate" type="xsd:date"/> </xsd:complexType>  <xsd:complexType name="USAddress"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="street" type="xsd:string"/> <xsd:element name="city" type="xsd:string"/> <xsd:element name="state" type="xsd:string"/> <xsd:element name="zip" type="xsd:decimal"/> </xsd:sequence> <xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/> </xsd:complexType>

Page 8: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

XML Schema for Example File <xsd:complexType name="Items"> <xsd:sequence> <xsd:element name="item" minOccurs="0"

maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="productName" type="xsd:string"/> <xsd:element name="quantity"> <xsd:simpleType> <xsd:restriction base="xsd:positiveInteger"> <xsd:maxExclusive value="100"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="USPrice" type="xsd:decimal"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="shipDate" type="xsd:date"

minOccurs="0"/> </xsd:sequence> <xsd:attribute name="partNum" type="SKU" use="required"/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType>

Page 9: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

XML Schema for Example File <!-- Stock Keeping Unit, a code for identifying

products --> <xsd:simpleType name="SKU"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{3}-[A-Z]{2}"/> </xsd:restriction> </xsd:simpleType> </xsd:schema>

Page 10: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Definitions and Declarations

• Complex types allow elements and attributes.• Simple types cannot have element content and

cannot have attributes. • Definitions create new types (both simple and

complex).• Declarations enable elements and attributes with

specific names and types (both simple and complex) to appear in document instances.

Page 11: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Defining the USAddress Type <xsd:complexType name="USAddress" > <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="street" type="xsd:string"/> <xsd:element name="city" type="xsd:string"/> <xsd:element name="state" type="xsd:string"/> <xsd:element name="zip" type="xsd:decimal"/> </xsd:sequence> <xsd:attribute name="country" type="xsd:NMTOKEN"

fixed="US"/> </xsd:complexType><billTo country="US"> <name>Robert Smith</name> <street>8 Oak Avenue</street> <city>Old Town</city> <state>PA</state> <zip>95819</zip> </billTo>

Page 12: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Defining PurchaseOrderType<xsd:complexType name="PurchaseOrderType">

<xsd:sequence>

<xsd:element name="shipTo" type="USAddress"/>

<xsd:element name="billTo" type="USAddress"/>

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

<xsd:element name="items" type="Items"/>

</xsd:sequence>

<xsd:attribute name="orderDate" type="xsd:date"/>

</xsd:complexType> 

 

Observe: Attributes can only contain simple types.

Page 13: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Defining PurchaseOrderType

In writing

<xsd:element ref="comment“ minOccurs="0"/>

 

Comment must have been globally defined (not within element or type declaration).

 

 

Page 14: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Restricting Occurrences of Elements and Attributes

 • minOccurs, maxOccurs limit the number of

occurrences• Default is 1.

• Attributes can appear at most once.• use: required (e.g., partNum), optional, or

prohibited. • default attribute: for attributes, supply value if

missing in instance (attribute must be optional)• default element: values supplied only when

instance elements ARE EMPTY (not when MISSING).

Page 15: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Restricting Occurrences of Elements and Attributes: Fixed

 • fixed: ensure that the attributes and elements are

set to particular values (e.g., country is declared with a fixed value US). • Value appears: must be the fixed value.• Value does not appear: the fixed value is

supplied.• default and fixed cannot be concurrently used.

Page 16: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Elements(minOccurs,

maxOccurs) fixed, default 

Attributesuse, fixed, default

Meaning

(1, 1) -, -required, -, - element/attribute must appear once, it may

have any value (1, 1) 37, -required, 37, - element/attribute must appear once, its

value must be 37 (2, unbounded) 37, -N/aelement must appear twice or more, its

value must be 37

(0, 1) -, -optional, -, - element/attribute may appear once, it may

have any value (0, 1) 37, optional, 37, -element/attribute may appear once, if it

does appear its value must be 37, if it

does not appear its value is 37 (0, 1) -, 37optional, -, 37 element/attribute may appear once; if it

does not appear its value is 37, otherwise its value is that given

(0, 0) -, -prohibited, -, - element/attribute must not appear

Page 17: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Restrictions on Global Elements

• Appear at top level of the document

• Cannot use ref• Cannot use minOccurs, maxOccurs

• Cannot mention use

Page 18: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Restricting Simple Types

• Use facets for restricting instances.<xsd:simpleType name="myInteger">

<xsd:restriction base="xsd:integer"> <xsd:minInclusive value="10000"/> <xsd:maxInclusive value="99999"/>

</xsd:restriction>

</xsd:simpleType> • Many facets: length, minLength,

maxLength, pattern, enumeration, whiteSpace …

Page 19: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Restricting Simple Types with a pattern

• "\d{3}-[A-Z]{2}" that is read "three digits followed by a hyphen followed by two upper-case ASCII letters".

<xsd:simpleType name="SKU"> <xsd:restriction base="xsd:string">

<xsd:pattern value="\d{3}-[A-Z]{2}"/>

</xsd:restriction>

</xsd:simpleType>

Page 20: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Restricting Simple Types with an enumeration

• Restrict a simple type by enumerating possibilities.

<xsd:simpleType name="USState"> <xsd:restriction base="xsd:string">

<xsd:enumeration value="AK"/> <xsd:enumeration value="AL"/> < xsd:enumeration value="AR"/> <!-- and so on ... --> </xsd:restriction>

</xsd:simpleType>

Page 21: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Simple Types = Atomic Types, List Types, Union Types

• List types are comprised of sequences of atomic types.

• Three built-in list types: NMTOKENS, IDREFS, and ENTITIES.

• Can create new list types by derivation from existing atomic types.

• Cannot create list types from existing list types, nor from complex types.

Page 22: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

List Types

• Type definition::

<xsd:simpleType name="listOfMyIntType"> <xsd:list itemType="myInteger"/>

</xsd:simpleType> • Element with a conforming content:

<listOfMyInt>20003 15037 95977 95945

</listOfMyInt>

Page 23: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

List with Facets: List Type for Six US States

• <xsd:simpleType name="USStateList"> <xsd:list itemType="USState"/> </xsd:simpleType> • <xsd:simpleType name="SixUSStates">

<xsd:restriction base="USStateList"> <xsd:length value="6"/>

</xsd:restriction> </xsd:simpleType>

• Conforming element:<sixStates>PA NY CA NY LA AK</sixStates>

Page 24: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Union types: Union Type for Zip codes

<xsd:simpleType name="zipUnion"> <xsd:union memberTypes= "USState listOfMyIntType"/>

</xsd:simpleType>

• Conforming elements of type zipUnion:– <zips>CA</zips– <zips>95630 95977 95945</zips>– <zips>AK</zips>

Page 25: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Anonymous Type Definitions<xsd:complexType name="Items"> <xsd:sequence> <xsd:element name="item" minOccurs="0" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="productName" type="xsd:string"/> <xsd:element name="quantity"> <xsd:simpleType> <xsd:restriction base="xsd:positiveInteger"> <xsd:maxExclusive value="100"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="USPrice" type="xsd:decimal"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="shipDate" type="xsd:date"

minOccurs="0"/> </xsd:sequence> <xsd:attribute name="partNum" type="SKU" use="required"/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType>

Page 26: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

<xsd:element name="internationalPrice"> <xsd:complexType>

<xsd:simpleContent> <xsd:extension base="xsd:decimal"> <xsd:attribute name="currency" type="xsd:string"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element>

• Conforming element:<internationalPrice currency="EUR"

value="423.46"/>

Elements with a simple content and attributes

No elements

Page 27: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

<letterBody> <salutation>Dear Mr.<name>Robert Smith</name>.</salutation> Your order of <quantity>1</quantity> <productName>Baby Monitor</productName> shipped from our warehouse on <shipDate>1999-05-21</shipDate>. .... </letterBody>

Mixed Content Example

Page 28: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

<xsd:element name="letterBody"> <xsd:complexType mixed="true"> <xsd:sequence> <xsd:element name="salutation"> <xsd:complexType mixed="true"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="quantity" type="xsd:positiveInteger"/> <xsd:element name="productName" type="xsd:string"/> <xsd:element name="shipDate" type="xsd:date"

minOccurs="0"/> <!-- etc. --> </xsd:sequence> </xsd:complexType></xsd:element>

Mixed Content Schema

Page 29: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

<xsd:element name="internationalPrice"> <xsd:complexType> <xsd:complexContent> <xsd:restriction base="xsd:anyType"> <xsd:attribute name="currency" type="xsd:string"/> <xsd:attribute name="value" type="xsd:decimal"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType></xsd:element>

• Conforming element:<internationalPrice currency="EUR"

value="423.46"/>

Empty Content

Page 30: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

<xsd:complexType name="PurchaseOrderType"> <xsd:sequence> <xsd:choice> <xsd:group ref="shipAndBill"/> <xsd:element name="singleUSAddress" type="USAddress"/> </xsd:choice> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="items" type="Items"/> </xsd:sequence> <xsd:attribute name="orderDate" type="xsd:date"/></xsd:complexType>

<xsd:group name="shipAndBill"> <xsd:sequence> <xsd:element name="shipTo" type="USAddress"/> <xsd:element name="billTo" type="USAddress"/> </xsd:sequence></xsd:group>

Nested Choice and Sequence Groups (single or 2 addresses)

Page 31: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

<xsd:complexType name="PurchaseOrderType"> <xsd:all> <xsd:element name="shipTo" type="USAddress"/> <xsd:element name="billTo" type="USAddress"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="items" type="Items"/> </xsd:all> <xsd:attribute name="orderDate" type="xsd:date"/></xsd:complexType>

• Top-level. • Group's children must all be individual elements (no

groups).• Element in the content model may appear more than

once, i.e. the permissible values of minOccurs and maxOccurs are 0 and 1.

ALL GROUPS

Page 32: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

<xsd:attributeGroup name="ItemDelivery"> <xsd:attribute name="partNum" type="SKU"

use="required"/> <xsd:attribute name="weightKg"

type="xsd:decimal"/> <xsd:attribute name="shipBy"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="air"/> <xsd:enumeration value="land"/> <xsd:enumeration value="any"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute></xsd:attributeGroup>

Defining Attribute Groups

Page 33: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

<xsd:element name="item" minOccurs="0" maxOccurs="unbounded">

<xsd:complexType> <xsd:sequence> <xsd:element name="productName" type="xsd:string"/> <xsd:element name="quantity"> <xsd:simpleType> <xsd:restriction base="xsd:positiveInteger"> <xsd:maxExclusive value="100"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="USPrice" type="xsd:decimal"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/> </xsd:sequence> <!-- attributeGroup replaces individual declarations --> <xsd:attributeGroup ref="ItemDelivery"/> </xsd:complexType></xsd:element>

Using Attribute Groups

Page 34: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

• <xsd:element name="shipDate" type="xsd:date" nillable="true"/>

• <shipDate xsi:nil="true"></shipDate>

• An element with xsi:nil="true" may not have any element content but it may still have attributes.

• Useful for representing missing information.

NIL Values

Page 35: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Target Namespaces and Naming

• A target namespace identifies a collection (vocabulary) of type definitions and element declarations. Essentially a logical container.

• The author can decide whether or not the appearance of locally declared elements and attributes in an instance must be qualified by a namespace, using either an explicit prefix or implicitly by default.

Page 36: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Target Namespaces and Naming: The Big Picture

Schema Level:• The XML Schema

namespace (xs, xsd)• Possibly a target

namespace• Possibly a default

xmlns namespace (probably target)

• Possibly other prefixes+namespaces

• Decide on instance qualifications

Instance Level:• Possibly the XSI

namespace• Possibly a default

xmlns namespace (probably a target namespace)

• Probably some other namespace prefixes

• Implement prescribed qualifications.

Page 37: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

PO Revisited<?xml version="1.0" encoding="UTF-8"?>

<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:po="http://www.example.com/PO1" targetNamespace="http://www.example.com/PO1" elementFormDefault="unqualified" attributeFormDefault="unqualified">

<element name="purchaseOrder" type="po:PurchaseOrderType"/>

<element name="comment" type="string"/> <complexType name="PurchaseOrderType"> <sequence> <element name="shipTo" type="po:USAddress"/> <element name="billTo" type="po:USAddress"/> <element ref="po:comment" minOccurs="0"/> <!-- etc. -->

Refer to local instance elements/attributes

Page 38: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Conforming Instance<apo:purchaseOrder xmlns:apo="http://www.example.com/PO1" orderDate="1999-10-20"> <shipTo country="US"> <name>Alice Smith</name> <street>123 Maple Street</street> <!-- etc. --> </shipTo> <billTo country="US"> <name>Robert Smith</name> <street>8 Oak Avenue</street> <!-- etc. --> </billTo> <apo:comment>Hurry, my lawn is going wild!</apo:comment> <!-- etc. --></apo:purchaseOrder>

• elementFormDefault and attributeFormDefault require that the prefix is not applied to any of the locally declared elements such as shipTo.

Page 39: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Summary

•A sophisticated schema language.

•Namespaces play a major role.

•OO features (abstract classes).

•Deterministic characteristics.

Page 40: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Example Schema<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema targetNamespace="http://www.oded1.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.oded1.com">

<xsd:element name="salesdata"><xsd:complexType>

<xsd:sequence><xsd:element name="year" type="YearType"

minOccurs="0" maxOccurs="unbounded"/></xsd:sequence>

</xsd:complexType></xsd:element><xsd:complexType name="YearType">

<xsd:sequence><xsd:element name="theyear" type="xsd:string"/><xsd:element name="region" type="RegionType" minOccurs="0"

maxOccurs="unbounded"/></xsd:sequence>

</xsd:complexType>

Page 41: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Example

<xsd:complexType name="RegionType"><xsd:sequence>

<xsd:element name="name" type="xsd:string"/><xsd:element name="sales">

<xsd:complexType><xsd:simpleContent>

<xsd:extension base="xsd:int">

<xsd:attribute name="unit" type="xsd:string"/>

</xsd:extension></xsd:simpleContent>

</xsd:complexType></xsd:element>

</xsd:sequence></xsd:complexType>

</xsd:schema>

Page 42: Introduction to XML Schema Winter 2003. Sources XML Schema Part 1: Structures W3C Recommendation 2 May 2001,

Instance<?xml version="1.0" encoding="UTF-8"?>

<s:salesdata xmlns:s="http://www.oded1.com" xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation="http://www.oded1.com C:\

salesdata.xsd" >

<year><theyear>1996</theyear><region>

<name>central</name><sales unit="millions">34</sales>

</region><region>

<name>east</name><sales unit="millions">34</sales>

</region><region>

<name>west</name><sales unit="millions">12</sales>

</region></year>

</s:salesdata>