[XML] XML Schema

An XML Schema formalizes the constraints that apply to an XML document. An XML Schema is like a class and an XML document is an object or instance of that schema.

 

1. XML Schema

Schemas do the following for XML documents:

  • Validation
  • Documentation
  • Data Binding
  • Content Guide

 

2. Replacing the DTD – Motivation for XML schemas

People are dissatisfied with DTDs.

XML Schemas

  • are extensible to future additions
  • are richer and more useful than DTDs
  • are written in XML
  • support data types
  • support namespaces

 

3. DTD vs. XML Schema

XML Document?

  • DTD: No
  • Schema: Yes

Data Typing?

  • DTD: weak support
  • Schema: strong support

Range Check?

  • DTD: No
  • Schema: Yes

 

4. Purposes of the XML Schema

  • Specify the structure of the instance document
  • Specify each element and attribute: (ex) The “Score” element shall hold an integer with the range 0 to 100. (DTDs don’t do well with specifying data types like this.)

 

5. Example: from DTD to Schema

A simple XML document : “book.xml”

<?xml version="1.0" ?>
<book>
  <title>The Lord of the Rings</title>
  <author>J.R.R. Tolkien</author>
  <isbn>1572810556</isbn>
</book>

A DTD : “book.dtd”

<!ELEMENT book (title, author, isbn)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT isbn (#PCDATA)>
  • Line 1 defines the book element to have three elements: “title”, “author”, “isbn”.
  • Line 2-4 defines the “title” element to be of the type “#PCDATA”, the “author” element to be of the type “#PCDATA”, and so on…

An XML Schema : “book.xsd”

<?xml version="1.0" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema "
  targetNamespace="http://www.book.com "
  xmlns="http://www.book.com "
  elementFormDefault="qualified">
  <xs:element name="book">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="title" type="xs:string"/>
        <xs:element name="author" type="xs:string"/>
        <xs:element name="isbn" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
  • The “book” element is said to be of a complex type because it contains other elements.
  • The other elements (title, author. isbn) are said to be simple types because they do not contain other elements.

A reference to a DTD

<?xml version="1.0" ?>
<!DOCTYPE note SYSTEM "book.dtd">
<book>
  ...
</book>

A reference to an XML Schema

<?xml version="1.0" ?>
<book xmlns="http://www.book.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.book.com book.xsd" >
  ...
</book>

 

6. Creating Schema Documents

(1) XML Declaration: An XML schema is also an XML document.

<?xml version="1.0" ?>

(2) Document (root) Element of an XML Schema

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

(3) Attributes of the root element (“schema”)

  • attributeFormDefault: specifies whether local attribute declarations are “qualified” or “unqualified”. This sets the default value for the “form” attribute of “xs:attribute” elements. (the default is “unqualified”)
  • elementFormDefault: specifies whether local element declarations are “qualified” or “unqualified”.This sets the default value for the “form” attribute of “xs:element” elements. (the default is “unqualified”)
  • targetNamespace: specifies the namespace that this schema document describes. (the default is “None”)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.book.com"
xmlns="http://www.book.com"
elementFormDefault="qualified" >

 

7. Referencing a Schema in an XML Document

<book xmlns="http://www.book.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.book.com book.xsd" >
  • xmlns:xsi specifies the XML Schema Instance namespace.
  • xsi:schemaLocation attribute has two values. The first value is the namespace to use. The second value is the location of the XML schema to use for that namespace.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s