XSD - <schema> Element

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

<schema> element

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

<?xml version="1.0"?>
<xs:schema>
...
...
</xs:schema>

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

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
targetNamespace="http://www.codew3c.com"
xmlns="http://www.codew3c.com"
elementFormDefault="qualified">
...
...
</xs:schema>

Code Explanation:

The following snippet:

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

Displays 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.codew3c.com"

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

This snippet:

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

Indicates that the default namespace is "http://www.codew3c.com".

This snippet:

elementFormDefault="qualified"

Indicates that any element used in an XML instance document that is declared in this schema must be namespace qualified.

Referencing Schema in an XML document

This XML document contains references to XML Schema:

<?xml version="1.0"?>
<note xmlns="http://www.codew3c.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.codew3c.com note.xsd"
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>

Code Explanation:

The following snippet:

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

Specifies the default namespace declaration. This declaration informs the schema validator that all elements used in this XML document are declared in the namespace "http://www.codew3c.com".

Once you have a usable XML Schema instance namespace:

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

You can then use the schemaLocation attribute. This attribute has two values. The first value is the namespace to be used. The second value is the location of the XML schema for the namespace:

xsi:schemaLocation="http://www.codew3c.com note.xsd"