XML Schema

  • Specifies an arbitrarily complex syntax for a set of XML files (validation criteria)
  • An XML alternative to DTD
  • Fairly complicated; not easy to learn
  • XML Schema is an XML language
  • XML Schema consists of two parts
  • XML Structure Schema (relationships)
  • XML Datatype Schema (data formats)

XSD

  • XSD (XML Schema Definition) (.xsd)
  • An instance of an XML Schema

Namespaces

  • XML Namespace (xmlns)
  • Namespaces in the XSD control the validation of corresponding namespaces in the instance document (XML data file)
xmlns[:name]="remote-path-for-namespace"

Sample namespaces

  • xmlns  : Default namespace (<element>)
  • xmlns:xsd  : XML Schema Definition namespace (<xsd:element>)
  • xmlns:lib  : An arbitrarily-named namespace for the XSD (<lib:bookType>) (<lib:book>)
  • xmlns:xsi  : XML Schema Instance namespace

Sample XML Schemas

XMLSchema
  • XML Schema in which the following are defined:
  • schema, attribute, element, complexType, sequence, all, choice, ...
  • string, boolean, date, ...
targetNamespace
  • XML schema in which the following sample elements are defined:
  • book, author, title, ...
  • bookType, authorType, titleType, ...
XMLSchema-instance
  • XML Schema used by the instance document, in which the following are defined:
  • SchemaLocation, noNamespaceSchemaLocation, ...

Misc

  • Local elements and attributes can be unqualified.
  • Global elements and attributes have to be qualified.
elementFormDefault
  • elementFormDefault="[qualified, unqualified]"
  • Elements declared in the current schema must be namespace qualified / unqualified
attributeFormDefault
  • attributeFormDefault="[qualified, unqualified]"
  • Attributes declared in the current schema must be namespace qualified / unqualified

Sample XML Schemas

Embedded definitions

  • targetNamespace doesn't have to be qualified
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" (qualified XMLSchema: xsd)
targetNamespace="remote-path-for-namespace"
xmlns:cat="remote-path-for-namespace"> (qualified targetNamespace: cat)

<xsd:element name="catalog">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="cd" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="artist" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

Flat design using references

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" (qualified XMLSchema: xsd)
targetNamespace="remote-path-for-namespace"
xmlns:cat="remote-path-for-namespace"> (qualified targetNamespace: cat)

<!-- definition of simple types -->
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="artist" type="xsd:string"/>

<!-- definition of complex types -->
<xsd:element name="cd">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="cat:title"/>
<xsd:element ref="cat:artist"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<!-- define the root element -->
<xsd:element name="catalog">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="cat:cd" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

Flat design using named types

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" (qualified XMLSchema: xsd)
targetNamespace="remote-path-for-namespace"
xmlns:cat="remote-path-for-namespace"> (qualified targetNamespace: cat)

<!-- definition of simple types -->
<xsd:simpleType name="titleType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="32"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="artistType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="32"/>
</xsd:restriction>
</xsd:simpleType>

<!-- definition of complex types -->
<xsd:complexType name="cdType">
<xsd:sequence>
<xsd:element name="title" type="cat:titleType"/>
<xsd:element name="artist" type="cat:artistType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="catalogType">
<xsd:sequence>
<xsd:element name="cd" type="cat:cdType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>

<!-- define the root element -->
<xsd:element name="catalog" type="cat:catalogType"/>
</xsd:schema>

Qualified XMLSchema, qualified targetNamespace

  • No default namespace
  • Cluttered
  • cat: can be confusing
  • Works on PHP5
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="remote-path-for-namespace"
xmlns:cat="remote-path-for-namespace">

<!-- definition of simple types -->
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="artist" type="xsd:string"/>

<!-- definition of complex types -->
<xsd:element name="cd">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="cat:title"/>
<xsd:element ref="cat:artist"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<!-- define the root element -->
<xsd:element name="catalog">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="cat:cd" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

Qualified XMLSchema, default targetNamespace

  • Easy to understand, but cluttered
  • On PHP5, only seems to work for embedded definitions
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" (qualified XMLSchema: xsd)
targetNamespace="remote-path-for-namespace"
xmlns="remote-path-for-namespace" (default targetNamespace)
elementFormDefault="qualified">

<xsd:element name="catalog">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="cd" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="artist" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

Sample usage in XML files

  • Instance documents
  • After the XML declaration and before the root element
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="remote-path-for-namespace"> <!-- default namespace >
<cd>
<title>...</title>
<artist>...</artist>
</cd>
...
</catalog>
<?xml version="1.0" encoding="UTF-8"?>
<cat:catalog xmlns:cat="remote-path-for-namespace"> <!-- cat namespace >
<cd>
<title>...</title>
<artist>...</artist>
</cd>
...
</cat:catalog>
<book isbn="..."
xmlns="remote-path-for-namespace" (default namespace)
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" (xsi namespace)
xsi:SchemaLocation="remote-path-for-namespace (namespace)
local-path/filename.xsd"> (schema)
...
</book>
<book isbn="..."
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" (xsi namespace)
xsi:noNamespaceSchemaLocation="local-path/filename.xsd"> (schema)
...
</book>