XML Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.00

XML Files with Linked External XSD Files

This section provides a tutorial example on how to store XSD statements in an external file, which can be then linked to XML files. The external XSD file should also be a well-formed XML document.

XSD File: An XML file that contains XSD statements that define elements, attributes and entities of a schema for XML files.

Instead of including XSD statements inside an XML file, we can also store XSD statements in an XSD file, and link the XSD file to the XML file.

Let's write our first XSD file to define a schema for our "Hello world!" XML file. Here is the XSD file, hello.xsd:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <xsd:element name="p" type="xsd:string"/>
</xsd:schema>

This XSD file needs to be linked to the XML file, hello_xsd.xml:

<?xml version="1.0"?>
<p xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:noNamespaceSchemaLocation="hello.xsd">
Hello world!</p>

Note that:

  • The XSD file is linked to the XML file through the "xsi:noNamespaceSchemaLocation" attribute in the root element.

Sections in This Chapter

What Is XSD (XML Schema Definition)?

XML Files with Embedded XSD Statements

XML Files with Linked External XSD Files

Validating an XML File against a XSD Schema

Dr. Herong Yang, updated in 2009
XML Files with Linked External XSD Files