The following four constructs are the central ones in XML Schema:
<element name="name" type="type"/>For an instance of an element to be valid, it must be of the type associated with the element name;
<attribute name="name" type="type"/>For an instance of an attribute to be valid, it must be of the type associated with the attribute name. Since attributes have no structure in XML, their type must be simple.
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:
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)