Skip to content

What does an XML Schema look like?

How to structure an XML Schema (XSD) with a practical example, comparing it to DTD and demonstrating how to define complex and simple types for XML validation.

Let's see an example.

An example of XML File

Consider this XML document for the next examples:

book.xmlxml
<?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.dtdxml
<!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.xsdxml
<?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>

A Reference to a DTD

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

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