XSD - The <schema> Element

« Previous Chapter Next Chapter »

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 may contain some 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.qctutorials.com"
xmlns="http://www.qctutorials.com"
elementFormDefault="qualified">
...
...
</xs:schema>

The Below fragment:

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

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

This fragment:

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

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

This fragment:

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

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

This fragment:

elementFormDefault="qualified"

indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified.

Referencing a Schema in an XML Document

This XML document has a reference to an XML Schema:

<?xml version="1.0"?>

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

<to>Adheem</to>
<from>Mubarak</from>
<heading>Reminder</heading>
<body>Do the Work</body>
</note>

The Below fragment:

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

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

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.qctutorials.com note.xsd"

« Previous Chapter Next Chapter »

Have Any Suggestion? We Are Waiting To Hear from YOU!

Your Query was successfully sent!