XSD - <schema>Elements


The <schema> element is the root element of every XML Schema.


The <schema> Element

The <schema> element is the root element of every XML Schema:

<?xml version="1.0"?>

<xs:schema>
...
...
</xs:schema>

The <schema> element can contain attributes. A schema declaration often looks something like this:

<?xml version="1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.com"
xmlns="http://www.example.com"
elementFormDefault="qualified">
...
...
</xs:schema>

The following code snippet:

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

indicates that the elements and data types used in the schema come from the namespace "http://www.w3.org/2001/XMLSchema". It also specifies that elements and data types from the namespace "http://www.w3.org/2001/XMLSchema" should use the prefix xs:

This snippet:

targetNamespace="http://www.example.com"

shows that the elements defined by this schema (note, to, from, heading, body) come from the namespace: "http://www.example.com".

This snippet:

xmlns="http://www.example.com"

indicates that the default namespace is "http://www.example.com".

This snippet:

elementFormDefault="qualified"

indicates that any elements used in an XML instance document and declared in this schema must be namespace-qualified.


Referencing a Schema in an XML Document

This XML document contains a reference to an XML Schema:

<?xml version="1.0"?>

<note xmlns="http://www.example.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.com note.xsd">

<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

The following code snippet:

xmlns="http://www.example.com"

specifies the declaration of the default namespace. This declaration tells the schema validator that all elements used in this XML document are declared in the namespace "http://www.example.com".

Once you have the XML Schema instance namespace available:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

you can use the schemaLocation attribute. This 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:

xsi:schemaLocation="http://www.example.com note.xsd"
Other Extensions