Skip to main content

XSD: The <schema> element

The <schema> element is the root element of every XML Schema. Let's see how use it.

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. For example:

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

where:

  • 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.
  • targetNamespace="https://tutorialreference.com" indicates that the elements defined by this schema (note, to, from, heading, body.) come from the https://tutorialreference.com namespace.
  • xmlns="https://tutorialreference.com" indicates that the default namespace is https://tutorialreference.com.
  • elementFormDefault="qualified" indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified.
note

elementFormDefault can be qualified or unqualified:

  • qualified elements and attributes are in the targetNamespace of the schema
  • unqualified elements and attributes do not have a namespace

Referencing a Schema in an XML Document

This XML document has a reference to an XML Schema:

book.xml
<book
xmlns="https://tutorialreference.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://tutorialreference.com/xml book.xsd">
<title>A Great Book</title>
<author>Tom Nolan</author>
<publisher>Tutorial Reference</publisher>
</book>

where:

  • xmlns="https://tutorialreference.com" specifies the default namespace declaration. This declaration tells to the schema-validator that all the elements used in this XML document are declared in the https://www.w3schools.com" namespace.
  • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" declares a standard namespace prefix (xsi) for a core namespace used in the XML schema
  • xsi:schemaLocation="https://tutorialreference.com/xml book.xsd" The schemaLocation attribute has two values separated by a space: the first is the name space to use while the second value is the location of the XML schema to use for that namespace.