XML and friendsPart 2 - XML Schema
ELAG 2001workshop 8
Jan Erik Kofoed ©BIBSYS Library Automation
2
Schema• XML Schema
W3C Recommendation 2 May 2001
• Will replace DTD• Comprehensive system divided into:
– Structures– Data types
• Three parts:– Primer http://www.w3c.org/TR/xmlschema-0/– Structures http://www.w3c.org/TR/xmlschema-1/– Datatypes http://www.w3c.org/TR/xmlschema-2/
3
Purchase order example PO (1)<?xml version="1.0"?><purchaseOrder orderDate="1999-10-20" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="po.xsd"> <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> <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>
4
PO (2) – XML-file, part 1<?xml version="1.0"?>
<purchaseOrder orderDate="1999-10-20" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:noNamespaceSchemaLocation="po.xsd">
<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>
5
PO (3) XML-file, part 2<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>
6
PO (4) DTD<!ELEMENT purchaseOrder (shipTo, billTo, comment, items)>
<!ATTLIST purchaseOrder orderDate CDATA #REQUIRED>
<!ELEMENT shipTo (name, street, city, state, zip)>
<!ATTLIST shipTo country CDATA #REQUIRED>
<!ELEMENT billTo (name, street, city, state, zip)>
<!ATTLIST billTo country CDATA #REQUIRED>
<!ELEMENT comment (#PCDATA)>
<!ELEMENT items (item+)>
<!ELEMENT item (productName, quantity, USPrice, comment?, shipDate?)>
<!ATTLIST item partNum CDATA #REQUIRED>
<!ELEMENT USPrice (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT productName (#PCDATA)>
<!ELEMENT quantity (#PCDATA)>
<!ELEMENT shipDate (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT zip (#PCDATA)>
7
Schema structure
• Schema can be divided into:– Prologue– Element declarations– Type declarations
• Complex Type)
• Simple Type)
• Those may be nested in different manners!
8
PO (5) schema, overview<xsd:schema ...><xsd:annotation ...> ... </xsd:annotation><xsd:element name=”purchaseorder” ... /><xsd:element name=”comment” ... /> ...<xsd:complexType name=”PurchaseorderType”> ...</xsd:complexType><xsd:complexType name=”USAddress”> ...</xsd:complexType><xsd:complexType name=”Items”> ...</xsd:complexType><xsd:simpleType name=”SKU”> ... </xsd:simpleType></xsd:schema>
9
PO (6) schema, part 1<xsd:schema
xmlns:xsd="http://www.w3.org/2000/10/XMLSchema">
<xsd:annotation> <xsd:documentation> 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"/>
10
PO (7) schema, part 2 <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>
11
PO (8) schema, part 3 <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" use="fixed" value="US"/>
</xsd:complexType>
12
PO (9) schema, part 4 <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"/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType>
13
PO (10) schema, part 5
<!-- 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>
14
Components of a schema
• element – declaration of elements• attribute – declaration of attributes,
must be declared after the elements• complexType – allows a content of elements and
attributes • simpleType – does not allow a content of elements
and attributes• elements may refer to globally declared elements• elements may assign a certain type
15
Occurrence of elements and attributes
• Element– minOccurs– maxOccurs– fixed– default
• Attribute– use (optional | prohibited | required | default | fixed) – value
• See, Primer table 1
16
Simple Types (1)• 45 predefined types
• New types are made as extensions of those
• Restrictions define the base type
• ”Facet” used to define constraints
– length, minLength, maxLength
– pattern (regular expression)
– enumeration (list of values)
– whiteSpace (preserve | replace | collapse)
– maxInclusive, maxExclusive, minInclusive, minExclusive
– precision, scale
– duration, period
17
Simple types (2)• list
– defines a new type as a list of another <xsd:simpleType name=”list_of_numbers”> <xsd:list itemType=”xsd:integer”> </xsd:simpleType>
• union– defines a new type as a union of other types
<xsd:simpleType name=”codes”> <xsd:union memberTypes=”xsd:integer xsd:boolean”> </xsd:simpleType>
18
Documentation in schema
• annotation, may include– documentation (free text)– appinfo (information to processes)
19
Content models (1)
• Extension of simple types:– <xsd:complexType ...>
<xsd:simpleContent> <xsd:extension base=...>
• Mixed content:– <xsd:complexType mixed=”true”>
• Empty content– Drop declaration of elements in
<xsd:complexType>
20
Content models (2)• Sequence
– <xsd:sequence>
• Choice– <xsd:choice>
• All once in arbitrary order– <xsd:all>
• Groups– <xsd:group>– <xsd:attributeGroup>
21
Keys
• Unique value– <xsd:unique ...>
<xsd:selector xpath= ...> <xsd:field xpath= ...>
• Keys– <xsd:key ...>
<xsd:selector xpath= ...> <xsd:field xpath= ...>
22
Reference to keys
• Declares that one value shall be equal to another declared as key.:– <xsd:keyref refer=... >
<xsd:selector xpath= ...> <xsd:field xpath= ...>
23
Include other schemas
• Read another schema:– <xsd:include schemaLocation= ... />
• Read another schema and allow alterations:– <xsd:redefine schemaLocation= ...>
... all corrections must come here ...</xsd:redefine>
24
Namespace
• Example:<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema" xmlns="http://example.org/ns/books/" targetNamespace="http://example.org/ns/books/" elementFormDefault="qualified" attributeFormDefault="unqualified”
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="myxml.xsd"/>
25
Unknown content
Unknown content may be declared using any and anyAttribute:
<xsd:complexType name="descType" mixed="true"> <xsd:sequence> <xsd:any namespace=http://www.w3.org/1999/xhtml minOccurs="0" maxOccurs="unbounded” processContents="skip"/> </xsd:sequence> </xsd:complexType>
26
Using a Schema in XML
• Use one of two types of XMLSchema-Instance:
<book isbn="0836217462" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:library.xsd">
<book isbn="0836217462" xmlns="http://example.org/ns/books/" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:schemaLocation="http://example.org/ns/books/ file:library.xsd">
Top Related