Skip to main content

What does an XML Schema look like?

Let's see an example.

An example of XML File

Consider this XML document for the next examples:

book.xml
<?xml version="1.0"?>  
<book>
<title>A Great Book</title>
<author>Tom Nolan</author>
<publisher>Tutorial Reference</publisher>
</book>

An example of DTD File

The following example is a DTD file called "book.dtd" that defines the elements of the XML document above ("book.xml"):

book.dtd
<!DOCTYPE book [  
<!ELEMENT book (title, author, publisher)>
<!ELEMENT title(#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT publisher(#PCDATA)>
]>

An example of XML Schema

The following example is an XML Schema file called "book.xsd" that defines the elements of the XML document above ("book.xml"):

book.xsd
<?xml version="1.0"?>

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

<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="publisher" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>
note

The book element is a complex type because it contains other elements. The other elements (title, author, publisher) are simple types because they do not contain other elements.

You will learn more about simple and complex types in the following chapters.

A Reference to a DTD

This XML document has a reference to a DTD, using <DOCTYPE>

book.xml
<?xml version="1.0"?>  

<!DOCTYPE note SYSTEM
"https://tutorialreference.com/xml/book.dtd">

<book>
<title>A Great Book</title>
<author>Tom Nolan</author>
<publisher>Tutorial Reference</publisher>
</book>

A Reference to an XML Schema

This XML document has a reference to an XML Schema:

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