Skip to content

XSD: The <schema> element

How to use the <schema> root element in XML Schema Definition (XSD), including required attributes like xmlns, targetNamespace, and elementFormDefault, and how to reference schemas in XML documents.

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.xmlxml
<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.

Referencing a Schema in an XML Document

This XML document has a reference to an XML Schema:

book.xmlxml
<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.
Navigation