XSD - <schema> Element

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

<schema> element

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

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

<schema> The 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 fragment:

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

Displays 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 fragment:

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

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

This fragment:

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

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

This fragment:

elementFormDefault="qualified"

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

Referencing Schema in 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 fragment:

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

This declaration specifies the default namespace. 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 the available XML Schema instance namespace:

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 be used. The second value is the location of the XML schema for the namespace:

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