Next page Previous page Start of chapter End of chapter

Basic steps

The following four constructs are the central ones in XML Schema:

It is convenient to start with the simplest example and get it working immediately. In the following the instance document is the document containing the information to validate and the schema document is the document containing the schema information used to validate the instance document. Consider the following instance document:

<?xml version="1.0"?>
<author>Scott Means</author>

A valid schema for this document is as follows:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="author" type="xs:string"/>
</xs:schema>

Some observations follow:

It is common to associate the instance document explicitly with the schema document. Suppose we name biblio.xsd the schema document above. The instance document changes as follows:

<?xml version="1.0"?>
<author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="biblio.xsd">
  Scott Means
</author>

The association is made with the noNamespaceSchemaLocation attribute which belongs to the XMLSchema instance namespace. This attribute is used when the instance document in written in no namespace. Suppose we intend to use the namespace URI http://www.dimi.uniud.it/francesc/biblio with b: prefix for our application. Our instance document changes as follows:

<?xml version="1.0"?>
<b:author xmlns:b="http://www.dimi.uniud.it/francesc/biblio"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.dimi.uniud.it/francesc/biblio biblio.xsd">
  Scott Means
</b:author>

and the schema document changes as follows:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://www.dimi.uniud.it/francesc/biblio">
  <xs:element name="author" type="xs:string"/>
</xs:schema>

The changes are the following:

  1. in the instance document, we declare the namespace for our application and use it to qualify the element name author;
  2. in the instance document, we use the attribute schemaLocation to associate the schema to the instance document. The value of this attribute consists of two parts that are separated by whitespace: a namespace URI and a URI that locates the schema document;
  3. finally, in the schema document, we use the attribute targetNamespace to indicate the namespace of the application described by the schema.

Validating a document against an XML schema requires a validating parser that supports XML Schema such as the open source Xerces parser from the Apache Xerces Project. This is written in Java and includes, in the archive xercesSamples.jar, a command-line program jaxp.SourceValidator that can be used to validate. The syntax for jaxp.SourceValidator follows:

java jaxp.SourceValidator -i instance.xml -a schema.xsd

Another validating parser supporting XML Schema is xmllint from the Gnome project. The syntax for XML Schema validation is as follows:

xmllint --schema biblio.xsd biblio.xml

You may also validate a document against a schema using BaseX function validate:xsd as in the following example:

let $doc := doc('banking.xml')
let $schema := doc('banking.xsd')
return validate:xsd($doc, $schema)
Next page Previous page Start of chapter End of chapter
Caffè XML - Massimo Franceschet