Datypic Logo
Definitive XML Schema

Definitive XML Schema

Priscilla Walmsley (pwalmsley@datypic.com)

1st edition (December 7, 2001)

Prentice Hall PTR; ISBN: 0130655678

Chapter 5: Instances and schemas

Full example

This example shows how xsi:schemaLocation can be used to pull together multiple schema documents. An import is used in chapter05ord.xsd to show the dependence on the prod namespace, but no schema location is provided in the schema; this type of "dangling" reference to product is allowed. This example also exhibits xsi:nil and xsi:type.

Instance (chapter05.xml)
<ord:order xmlns:ord="http://example.org/ord"
   xmlns:prod="http://example.org/prod"
   xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://example.org/prod chapter05prod.xsd
                       http://example.org/ord chapter05ord.xsd">
  <items>
    <prod:product>
      <number xsi:type="xs:short">557</number>
      <name>Short-Sleeved Linen Blouse</name>
      <size xsi:nil="true"></size>
    </prod:product>
  </items>
</ord:order>
Schema Document 1 (chapter05ord.xsd)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://example.org/ord"
            xmlns="http://example.org/ord"
            xmlns:prod="http://example.org/prod">
  <xs:import namespace="http://example.org/prod"/>
  <xs:element name="order" type="OrderType"/>
  <xs:complexType name="OrderType">
    <xs:sequence>
      <xs:element name="items" type="ItemsType"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="ItemsType">
    <xs:sequence>
      <xs:element ref="prod:product" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
Schema Document 2 (chapter05prod.xsd)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/prod"
            targetNamespace="http://example.org/prod">
  <xs:element name="product" type="ProductType"/>
  <xs:complexType name="ProductType">
    <xs:sequence>
      <xs:element name="number" type="xs:integer"/>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="size" nillable="true" type="SizeType"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="SizeType">
    <xs:simpleContent>
      <xs:extension base="xs:integer">
        <xs:attribute name="system" type="xs:string"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:schema>
Datypic XML Schema Services

Book examples

Example 5-1. Using an instance attribute
<product xmlns="http://example.org/prod"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <number xsi:type="ShortProdNumType">557</number>
  <size>10</size>
</product>
Example 5-2. Using xsi:schemaLocation
<product xmlns="http://example.org/prod"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://example.org/prod prod.xsd">
  <number>557</number>
  <size>10</size>
</product>
Example 5-3. Using xsi:schemaLocation with multiple pairs
<order xmlns="http://example.org/ord"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://example.org/prod prod.xsd
                           http://example.org/ord ord1.xsd">
  <items>
    <product xmlns="http://example.org/prod">
      <number>557</number>
      <size>10</size>
    </product>
  </items>
</order>
Example 5-4. Using xsi:noNamespaceSchemaLocation
<product xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="prod.xsd">
  <number>557</number>
  <size>10</size>
</product>
Example 5-5. RDDL for the product catalog namespace
<?xml version='1.0'?>
<!DOCTYPE html PUBLIC "-//XML-DEV//DTD XHTML RDDL 1.0//EN"
                      "rddl/rddl-xhtml.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:xlink="http://www.w3.org/1999/xlink"
      xmlns:rddl="http://www.rddl.org/">
<head><title>Product Catalog</title></head>
<body><h1>Product Catalog</h1>
  <div id="toc"><h2>Table of Contents</h2>
    <ol>
      <li><a href="#intro">Introduction</a></li>
      <li><a href="#related.resources">Resources</a></li>
    </ol>
  </div>
  <div id="intro"><h2>Introduction</h2>
    <p>This document describes the <a href="#xmlschemap1">Product
      Catalog</a> namespace and contains a directory of links to
      related resources.</p>
  </div>
  <div id="related.resources">
    <h2>Related Resources for the Product Catalog Namespace</h2>
    <!-- start resource definitions -->
  <div class="resource" id="DTD">
    <rddl:resource xlink:title="DTD for validation"
     xlink:arcrole="http://www.rddl.org/purposes#validation"
     xlink:role="http://www.isi.edu/in-
                 notes/iana/assignments/media-types/text/xml-dtd"
     xlink:href="prod.dtd">
      <h3>DTD</h3>
      <p>A <a href="prod.dtd">DTD</a> for the Product Catalog.</p>
    </rddl:resource>
  </div>
  <div class="resource" id="xmlschema">
    <rddl:resource xlink:title="Products schema"
     xlink:role="http://www.w3.org/2001/XMLSchema"
     xlink:arcrole="http://www.rddl.org/purposes#schema-validation"
     xlink:href="prod.xsd">
      <h3>XML Schema</h3>
      <p>An <a href="prod.xsd">XML Schema</a> for the Product
         Catalog.</p>
    </rddl:resource>
  </div>
  <div class="resource" id="documentation">
    <rddl:resource xlink:title="Application Documentation"
     xlink:role="http://www.w3.org/TR/html4"
     xlink:arcrole="http://www.rddl.org/purposes#reference"
     xlink:href="prod.html">
      <h3>Application Documentation</h3>
      <p><a href="prod.html">Application documentation</a> for
          the Product Catalog application.</p>
    </rddl:resource>
  </div>
  </div>
</body></html>
Example 5-6. A valid instance?
<number>557</number>
Example 5-7. Using a DTD and a schema
<!NOTATION jpeg SYSTEM "JPG">
<!ENTITY prod557 SYSTEM "prod557.jpg" NDATA jpeg>
<!ENTITY prod563 SYSTEM "prod563.jpg" NDATA jpeg>]>
<catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="prod.xsd">
  <product>
    <number>557</number>
    <picture location="prod557"/>
  </product>
  <product>
    <number>563</number>
    <picture location="prod563"/>
  </product>
</catalog>
Example 5-8. Java code to set schema validation in Xerces
SAXParser p=new SAXParser();
try {
// Turn schema validation on
p.setFeature
 ("http://xml.org/sax/features/validation", true);
p.setFeature
 ("http://apache.org/xml/features/validation/schema", true);
p.setFeature
 ("http://apache.org/xml/features/validation/schema-full-checking",
   true);
} catch (SAXException e) {
  System.out.println("error turning on validation");
}
Example 5-9. Java code to launch Oracle XML Parser
import oracle.xml.parser.schema.*;
import oracle.xml.parser.v2.*;
...
DOMParser dp = new DOMParser();
...
// Turn schema validation on
dp.setValidationMode(XMLParser.SCHEMA_VALIDATION);
...
// Assign a particular schema
XSDBuilder builder = new XSDBuilder();
XMLSchema schemadoc = (XMLSchema)builder.build("prod.xsd");
dp.setXMLSchema(schemadoc);
Example 5-10. JavaScript function to launch MSXML
<function runParser(xmlFileName,namespace,xsdFileName)
  {
  var xmlschema = new ActiveXObject("Msxml2.XMLSchemaCache.4.0");
  xmlschema.add(namespace,xsdFileName);
  var xmldoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
  xmldoc.async = false;
  xmldoc.schemas = xmlschema;
  xmldoc.load(xmlFileName);
  if(xmldoc.parseError.errorCode != 0)
    output.innerHTML= xmldoc.parseError.errorCode +
                xmldoc.parseError.reason;
  else
    output.innerHTML="No errors.">
  }
Datypic XML Schema Services