XML Schema
A successor to DTD XML Schema is a successor to DTD Like DTD, it is used to specify the tags and attributes of an XML-based mark-up language But, unlike DTD, XML Schema has an XML-based syntax itself It is also more powerful that DTD, because It supports data types It supports namespaces
An Example Before we consider the XML Schema language in detail, let’s consider an example usage We will see an XML Schema file We will see two XML documents which claim to be valid according to this schema But only one of them is valid We will a use a tool which identifies the error
Example XML Schema Location: http://cosmos.ucc.ie/cs1064/jabowen/IPSC/cs4408/xml/schema/memo.xsd Contents: <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://cosmos.ucc.ie" elementFormDefault="qualified"> <xs:element name="memo"> <xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Example XML document Location: http://cosmos.ucc.ie/cs1064/jabowen/IPSC/cs4408/xml/schema/memo1.xml Contents: <?xml version="1.0" ?> <memo xmlns="http://cosmos.ucc.ie" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://cosmos.ucc.ie http://cosmos.ucc.ie/cs1064/jabowen/IPSC/cs4408/xml/schema/memo.xsd"> <to>Fred</to> <from>Bill</from> <heading>Meeting tomorrow</heading> <body>It will be in Room 209</body> </memo>
2nd example XML document Location: http://cosmos.ucc.ie/cs1064/jabowen/IPSC/cs4408/xml/schema/memo2.xml Contents: <?xml version="1.0" ?> <memo xmlns="http://cosmos.ucc.ie" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://cosmos.ucc.ie http://cosmos.ucc.ie/cs1064/jabowen/IPSC/cs4408/xml/schema/memo.xsd"> <to>Fred</to> <from>Bill</from> <date>May 2, 2008</date> <heading>Meeting tomorrow</heading> <body>It will be in Room 209</body> </memo>
An XML Schema validator The W3C provide an online tool for checking whether an XML document is valid according to an XML Schema Location: http://www.w3.org/2001/03/webdata/xsv
Checking memo1.xml
Checking memo2.xml
A simple XML document <?xml version="1.0" ?> <memo> <to>Fred</to> <from>Bill</from> <heading>Meeting tomorrow</heading> <body>It will be in Room 209</body> </memo>
Referring to a schema <?xml version="1.0" ?> <memo xmlns="http://cosmos.ucc.ie" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://cosmos.ucc.ie http://cosmos.ucc.ie/cs1064/jabowen/IPSC/cs4408/xml/schema/memo.xsd “ > <memo> <to>Fred</to> <from>Bill</from> <heading>Meeting tomorrow</heading> <body>It will be in Room 209</body> </memo>
Examining a schema reference <?xml version="1.0" ?> <memo xmlns="http://cosmos.ucc.ie" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://cosmos.ucc.ie http://cosmos.ucc.ie/cs1064/jabowen/IPSC/cs4408/xml/schema/memo.xsd “ > The attribute xmlns=“http://cosmos.ucc.ie” specifies that the tag memo belongs to the namespace http://cosmos.ucc.ie
Examining a schema reference (contd.) <?xml version="1.0" ?> <memo xmlns="http://cosmos.ucc.ie" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://cosmos.ucc.ie http://cosmos.ucc.ie/cs1064/jabowen/IPSC/cs4408/xml/schema/memo.xsd “ > The attribute xsi:schemaLocation=“http://cosmos.ucc.ie ... ” specifies that the schema for the namespace http://cosmos.ucc.ie is at this URL:
Examining a schema reference (contd. again) <?xml version="1.0" ?> <memo xmlns="http://cosmos.ucc.ie" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://cosmos.ucc.ie http://cosmos.ucc.ie/cs1064/jabowen/IPSC/cs4408/xml/schema/memo.xsd “ > The attribute xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” specifies the URI for the namespace xsi which is needed for the schemaLocation attribute
The <schema> element The root element in every XML Schema is the <schema> element It usually contains some attributes, which will be discussed on the following slides Example XML Schema file <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://cosmos.ucc.ie" elementFormDefault="qualified“ > ... ... </xs:schema>
Attributes of the <schema> element Example open-tag for <schema> element <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://cosmos.ucc.ie" elementFormDefault="qualified“ > The xmlns:xs attribute is needed to specify the URI for the xs prefix which is used with the schema tag name
Attributes of the <schema> element (contd.) Example open-tag for <schema> element <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://cosmos.ucc.ie" elementFormDefault="qualified“ > The targetNamespace attribute specifies that this schema is defining the tags and attributes for the namespace whose URI is http://cosmos.ucc.ie
Attributes of the <schema> element (contd.) Example open-tag for <schema> element <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://cosmos.ucc.ie" elementFormDefault="qualified“ > The elementFormDefault attribute specifies that XML documents which use this schema must be namespace-qualified
Contents of a <schema> element You will remember that a DTD file contains <!ELEMENT> and <!ATTLIST> declarations Similarly, a XML schema element contains <element> and <attribute> children elements
The <element> element In the XML Schema view of things, there are two types of elements Simple Complex A simple element Can only contain text content – it cannot contain any other elements Also, it cannot have any attributes. A complex element Can contain different kinds of content Can have attributes
Simple elements
Types of simple element As said before, a simple element can only contain text HOWEVER, one advantage of XML Schema over DTD is that we can specify restrictions on the type of text which a simple element can contain The most common types of text are: string decimal integer boolean date time
Defining a simple element Syntax: <xs:element name="xxx" type="yyy"/> Examples extracted from earlier schema <xs:element name="to" type="xs:string"/> <xs:element name=“from" type="xs:string"/> <xs:element name=“heading" type="xs:string"/> <xs:element name=“body" type="xs:string"/> Other examples <xs:element name=“age" type="xs:integer"/> <xs:element name=“dateofBirth" type="xs:date"/>
Revisiting our 2nd example XML document This failed because it included a date Location: http://cosmos.ucc.ie/cs1064/jabowen/IPSC/cs4408/xml/schema/memo2.xml Contents: <?xml version="1.0" ?> <memo xmlns="http://cosmos.ucc.ie" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://cosmos.ucc.ie http://cosmos.ucc.ie/cs1064/jabowen/IPSC/cs4408/xml/schema/memo.xsd"> <to>Fred</to> <from>Bill</from> <date>May 2, 2008</date> <heading>Meeting tomorrow</heading> <body>It will be in Room 209</body> </memo>
Extended schema which includes a date Location: http://cosmos.ucc.ie/cs1064/jabowen/IPSC/cs4408/xml/schema/extendedMemo.xsd Contents: <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://cosmos.ucc.ie" elementFormDefault="qualified"> <xs:element name="memo"> <xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name=“date" type="xs:date"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Using this new schema Location: http://cosmos.ucc.ie/cs1064/jabowen/IPSC/cs4408/xml/schema/memo2a.xml Contents: <?xml version="1.0" ?> <memo xmlns="http://cosmos.ucc.ie" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://cosmos.ucc.ie http://cosmos.ucc.ie/cs1064/jabowen/IPSC/cs4408/xml/schema/extendedMemo.xsd"> <to>Fred</to> <from>Bill</from> <date>May 2, 2008</date> <heading>Meeting tomorrow</heading> <body>It will be in Room 209</body> </memo>
Checking this document: still fails
Online description of XML Schema For more information: http://www.w3.org/XML/Schema
Using the date data type correctly An xs:date: must be specified in one of the forms: yyyy-mm-dd yyyy-mm-ddZ, where Z means GMT yyyy-mm-dd-hh:mm yyyy-mm-dd+hh:mm
Using the date data type correctly Location: http://cosmos.ucc.ie/cs1064/jabowen/IPSC/cs4408/xml/schema/memo2b.xml Contents: <?xml version="1.0" ?> <memo xmlns="http://cosmos.ucc.ie" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://cosmos.ucc.ie http://cosmos.ucc.ie/cs1064/jabowen/IPSC/cs4408/xml/schema/extendedMemo.xsd"> <to>Fred</to> <from>Bill</from> <date>2008-05-02</date> <heading>Meeting tomorrow</heading> <body>It will be in Room 209</body> </memo>
Now the document is valid
The time date type A start time could be defined in a schema as follows <xs:element name="start" type="xs:time"/> An xs:time: must be specified in one of the forms: hh:mm:ss hh-mm-ssZ, where Z means GMT hh-mm-ss-hh:mm hh-mm-ss+hh:mm Valid instances would include <start>09:10:10</start> <start>09:30:10.5</start> <start>09:10:10Z</start> <start>09:30:10-06:00</start>
The dateTime date type A start date and time could be defined in a schema as follows <xs:element name="start" type="xs:dateTime"/> An xs:dateTime: must be specified in one of the forms: yyyy-mm-ddThh:mm:ss yyyy-mm-ddThh:mm:ssZ, where Z means GMT yyyy-mm-ddThh:mm:ss-hh:mm yyyy-mm-ddThh:mm:ss+hh:mm Valid instances would include <start>2010-12-24T09:10:10</start> <start>2010-12-24T09:30:10.5</start> <start>2010-12-24T09:10:10Z</start> <start>2010-12-24T09:30:10-06:00</start>
Other time and date data types There are other time and date data types But we will not consider them here
The decimal date type A sum of money could be defined in a schema as follows <xs:element name="salary" type="xs:decimal"/> An xs:decimal: must be specified in one of the forms: ddddddd.dddddd -ddddddd.dddddd +ddddddd.dddddd dddddddd Valid instances would include <accountBalance>3599.57</accountBalance> <accountBalance>+1500.67</accountBalance> <accountBalance>-124.50</accountBalance> <accountBalance>990</accountBalance> <accountBalance>0</accountBalance>
The integer date type A counter could be defined in a schema as follows <xs:element name=“numChildren" type="xs:integer"/> An xs:decimal: must be specified in one of the forms: ddddddd -ddddddd +ddddddd There are other numeric data types but we will not consider them here.
Default and Fixed Values The specification of a simple element type may include a default value OR a fixed value A default value is automatically assigned to an instance of the element type when no other value is specified. <xs:element name="colour" type="xs:string" default="red"/> A fixed value is always automatically assigned to an instance of the element type and no other value can be specified <xs:element name="colour" type="xs:string" fixed="red"/>
Complex elements
A complex element is one that either has attributes or Complex elements A complex element is one that either has attributes or contains other elements, or both Example complex elements <employee id="1345"/> <name><fname>Bertie</fname><sname>Ahern</sname></name> <employee id="1345"/>Bertie Ahern</employee> <text>The capital of Ireland is <city>Dublin</city></text> A complex element can be empty, can have child elements, can contain only text, or can contain a mixture of text and child elements
Specifying complex elements A complex element type can be specified in two different ways, “private” and “public” Example use of “private” complexType specification <xs:element name=“lecturer"> <xs:complexType> … </xs:complexType> </xs:element> Example use of “public” complexType specification <xs:complexType name="person">…</xs:complexType> <xs:element name=“lecturer" type="person"/> <xs:element name="student" type="person"/> Notice that, in the public specification, the complexType is given a name which can be referenced by several element types, whereas in the private specification, the complexType has no name and is hidden inside one element type specification
Specifying complex elements (contd.) A public complexType can also be used as a base for defining other public complexTypes Example “public” specification <xs:element name=“lecturer" type=“employee"/> <xs:element name="student" type="person"/> <xs:complexType name="person">…</xs:complexType> <xs:complexType name=“employee“> <xs:complexContent> <xs:extension base="person“> … </xs:extension> </xs:complexContent> </xs:complexType>
Specifying and extending a complex elements Specifying a complex type <xs:complexType name="person“> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> Extending a complex type <xs:complexType name=“employee“> <xs:complexContent> <xs:extension base="person“> <xs:sequence> <xs:element name=“grade" type="xs:string"/> <xs:element name=“salary" type="xs:decimal"/> </xs:sequence> </xs:extension> </xs:complexContent>
Specifying an empty complex element Suppose we want to have the following type of element in our XML documents: <model modelid=“999" /> We can specify this empty complex type as follows <xs:element name=“model"> <xs:complexType> <xs:attribute name=“modelid“ type="xs:positiveInteger"/> </xs:complexType> </xs:element>
Specifying a complex element with only text content Suppose we want to have the following type of element in our XML documents: <countryCode country=“Ireland“>353</countryCode> We can specify this empty complex type as follows <xs:element name=“countryCode"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:integer"> <xs:attribute name="country" type="xs:string" /> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element>
Specifying a complex element with mixed content Suppose we want to have the following type of element in our XML documents: <note>Dear<person>John></person> we will <operation>contact you soon</operation></note> We can specify this empty complex type as follows <xs:element name=“note"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name=“person" type="xs:string"/> <xs:element name=“operation" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>