Latest web development tutorials

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

The following code fragment:

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

Used in display schema elements and data types from the namespace "http://www.w3.org/2001/XMLSchema". It also sets out from the namespace "http://www.w3.org/2001/XMLSchema" elements and data types should use the prefix xs:

This snippet:

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

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

This snippet:

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

It states that the default namespace is "http://www.w3big.com".

This snippet:

elementFormDefault="qualified"

Identify any XML instance document used and the element declared in this schema must be over-defined namespace.


Schema referenced in the XML document

This XML document contains a reference to the XML Schema:

<?xml version="1.0"?>

<note xmlns="http://www.w3big.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3big.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.w3big.com"

It specifies a default namespace declaration. This declaration tells the schema validator that all the elements used in this XML document are declared in "http://www.w3big.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 property has two values. The first value is the namespace to use. The second value is the location for XML schema namespaces used:

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