XSD Tutorials - Herong's Tutorial Examples - Version 5.10, by Dr. Herong Yang

Assigning XML Schema to XML Documents

This section describes a tutorial example on how to assign an XML Schema file (XSD) to an XML document using XMLPad.

If you want to validate an XML document against an XSD (XML Schema Definition) document, you need to assign (link) the XSD file to the XML document. Since we have created a simple XSD document, first_html.xsd, and a simple XML document, first_html.xml, that should conform to the rules defined in first_html.xsd, let's try to assign first_html.xsd to first_html.xml with XMLPad:

1. Run XMLPad and click menu File > Open. Select c:\herong\first_html.xml in the "Open" dialog box. XMLPad displays the XML document in the edit window:

<?xml version = "1.0" encoding = "utf-8"?>
<html>
<body>My first HTML document in XML format.</body>
</html>

2. Click menu XML > Assign Schema/DTD. The "Assign Schema/DTD" dialog box shows up.

3. Check the "W3C Schema" radio button and the "Browse..." button to select the XSD document c:\herong\first_html.xsd.
XMLPad Assign XSD Schema

4. Leave the Namespace field blank and click OK. XMLPad will modify the root element "html" to insert two attributes to assign the XSD file, first_html, as the validation schema for first_html.xml.

<?xml version="1.0" encoding="utf-8"?>
<html xsi:noNamespaceSchemaLocation="file:///C:/herong/first_html.xsd"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<body>My first HTML document in XML format.</body>
</html>

5. The last step is to click menu File > Save As. And save this modified XML document as c:\herong\first_html_xsd.xml.

To fully understand this XSD document, you need to read other chapters in book to learn XML Schema language syntax. But here is quick description what the two extra attributes are:

  • "xmlns:xsi" defines a namespace called "xsi" to be used as a prefix for element and attribute names.
  • "xsi:noNamespaceSchemaLocation" defines where is the XSD file located.

Last update: 2013.

Table of Contents

 About This Book

 Introduction to XML Schema

XML Editor and Schema Processor - XMLPad

 Installing WMHelp XMLPad Pro

 Creating and Editing XML Documents

 Creating and Editing XML Schema Documents

Assigning XML Schema to XML Documents

 Validating XML Documents with Assigned XSD

 Validating Non-Conforming XML Documents

 XMLPad Crash When Validating Non-Conforming XML Documents

 Generating XML Schema Diagrams

 Generating XML Schema Documentations

 Java API for XML Processing - JAXP

 JAXP - XML Schema (XSD) Validation

 Xerces2 Java Parser - Java API of XML Parsers

 Using Xerces2 Java API

 XML Schema Language - Basics

 Introduction of XSD Built-in Datatypes

 "string" and Its Derived Datatypes

 "decimal" and Its Derived Datatypes

 "dateTime" and Its Related Datatypes

 Miscellaneous Built-in Datatypes

 Facets, Constraining Facets and Restriction Datatypes

 "simpleType" - Defining Your Own Simple Datatypes

 Complex Element Declaration

 Identity-Constraints: unique, key and keyref

 Assertion as Custom Validation Rules

 XML Schema Location and Namespace in XML Documents

 Overriding Element Types in XML Documents

 Linking Multiple Schema Documents Together

 Glossary

 References

 PDF Printing Version

Assigning XML Schema to XML Documents - Updated in 2014, by Dr. Herong Yang