Presentation is loading. Please wait.

Presentation is loading. Please wait.

XML Schema.

Similar presentations


Presentation on theme: "XML Schema."— Presentation transcript:

1 XML Schema

2 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

3 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

4 Example XML Schema Location: Contents: <?xml version="1.0"?> <xs:schema xmlns:xs=" targetNamespace=" 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>

5 Example XML document Location: Contents: <?xml version="1.0" ?> <memo xmlns=" xmlns:xsi=" xsi:schemaLocation=" <to>Fred</to> <from>Bill</from> <heading>Meeting tomorrow</heading> <body>It will be in Room 209</body> </memo>

6 2nd example XML document
Location: Contents: <?xml version="1.0" ?> <memo xmlns=" xmlns:xsi=" xsi:schemaLocation=" <to>Fred</to> <from>Bill</from> <date>May 2, 2008</date> <heading>Meeting tomorrow</heading> <body>It will be in Room 209</body> </memo>

7 An XML Schema validator
The W3C provide an online tool for checking whether an XML document is valid according to an XML Schema Location:

8 Checking memo1.xml

9 Checking memo2.xml

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

11 Referring to a schema <?xml version="1.0" ?> <memo
xmlns=" xmlns:xsi=" xsi:schemaLocation= " > <memo> <to>Fred</to> <from>Bill</from> <heading>Meeting tomorrow</heading> <body>It will be in Room 209</body> </memo>

12 Examining a schema reference
<?xml version="1.0" ?> <memo xmlns=" xmlns:xsi=" xsi:schemaLocation= " > The attribute xmlns=“ specifies that the tag memo belongs to the namespace

13 Examining a schema reference (contd.)
<?xml version="1.0" ?> <memo xmlns=" xmlns:xsi=" xsi:schemaLocation= " > The attribute xsi:schemaLocation=“ ” specifies that the schema for the namespace is at this URL:

14 Examining a schema reference (contd. again)
<?xml version="1.0" ?> <memo xmlns=" xmlns:xsi=" xsi:schemaLocation= " > The attribute xmlns:xsi=“ specifies the URI for the namespace xsi which is needed for the schemaLocation attribute

15 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=" targetNamespace=" elementFormDefault="qualified“ > </xs:schema>

16 Attributes of the <schema> element
Example open-tag for <schema> element <?xml version="1.0"?> <xs:schema xmlns:xs=" targetNamespace=" elementFormDefault="qualified“ > The xmlns:xs attribute is needed to specify the URI for the xs prefix which is used with the schema tag name

17 Attributes of the <schema> element (contd.)
Example open-tag for <schema> element <?xml version="1.0"?> <xs:schema xmlns:xs=" targetNamespace=" elementFormDefault="qualified“ > The targetNamespace attribute specifies that this schema is defining the tags and attributes for the namespace whose URI is

18 Attributes of the <schema> element (contd.)
Example open-tag for <schema> element <?xml version="1.0"?> <xs:schema xmlns:xs=" targetNamespace=" elementFormDefault="qualified“ > The elementFormDefault attribute specifies that XML documents which use this schema must be namespace-qualified

19 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

20 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

21 Simple elements

22 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

23 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"/>

24 Revisiting our 2nd example XML document
This failed because it included a date Location: Contents: <?xml version="1.0" ?> <memo xmlns=" xmlns:xsi=" xsi:schemaLocation=" <to>Fred</to> <from>Bill</from> <date>May 2, 2008</date> <heading>Meeting tomorrow</heading> <body>It will be in Room 209</body> </memo>

25 Extended schema which includes a date
Location: Contents: <?xml version="1.0"?> <xs:schema xmlns:xs=" targetNamespace=" 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>

26 Using this new schema Location: Contents: <?xml version="1.0" ?> <memo xmlns=" xmlns:xsi=" xsi:schemaLocation= " <to>Fred</to> <from>Bill</from> <date>May 2, 2008</date> <heading>Meeting tomorrow</heading> <body>It will be in Room 209</body> </memo>

27 Checking this document: still fails

28 Online description of XML Schema
For more information:

29 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

30 Using the date data type correctly
Location: Contents: <?xml version="1.0" ?> <memo xmlns=" xmlns:xsi=" xsi:schemaLocation= " <to>Fred</to> <from>Bill</from> <date> </date> <heading>Meeting tomorrow</heading> <body>It will be in Room 209</body> </memo>

31 Now the document is valid

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

33 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> T09:10:10</start> <start> T09:30:10.5</start> <start> T09:10:10Z</start> <start> T09:30:10-06:00</start>

34 Other time and date data types
There are other time and date data types But we will not consider them here

35 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> </accountBalance> <accountBalance> </accountBalance> <accountBalance> </accountBalance> <accountBalance>990</accountBalance> <accountBalance>0</accountBalance>

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

37 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"/>

38 Complex elements

39 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

40 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

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

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

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

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

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


Download ppt "XML Schema."

Similar presentations


Ads by Google