Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0132886723

2nd edition, , Prentice Hall PTR.

Chapter 19: Topics for DTD users

Book examples

Example 19-1. Simple type
DTD:
<!ELEMENT price (#PCDATA)>
Schema:
<xs:element name="price" type="xs:decimal"/>
Example 19-2. Simple content (with attributes)
DTD:
<!ELEMENT price (#PCDATA)>
<!ATTLIST price currency NMTOKEN #IMPLIED>
Schema:
<xs:element name="price">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:decimal">
        <xs:attribute name="currency" type="xs:NMTOKEN"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>
Example 19-3. Complex content
DTD:
<!ELEMENT product (number, name+, size?, color*)>
Schema:
<xs:element name="product">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="number"/>
      <xs:element ref="name"  maxOccurs="unbounded"/>
      <xs:element ref="size"  minOccurs="0"/>
      <xs:element ref="color" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
Example 19-4. Nested groups
DTD:
<!ELEMENT el ((a | b)*, (c | d)?)>
Schema:
<xs:element name="el">
  <xs:complexType>
    <xs:sequence>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="a"/>
        <xs:element ref="b"/>
      </xs:choice>
      <xs:choice minOccurs="0" maxOccurs="1">
        <xs:element ref="c"/>
        <xs:element ref="d"/>
      </xs:choice>
    </xs:sequence>
  </xs:complexType>
</xs:element>
Example 19-5. Mixed content
DTD:
<!ELEMENT letter (#PCDATA | custName | prodName)*>
Schema:
<xs:element name="letter">
  <xs:complexType mixed="true">
    <xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:element ref="custName"/>
      <xs:element ref="prodName"/>
    </xs:choice>
  </xs:complexType>
</xs:element>
Example 19-6. Empty content
DTD:
<!ELEMENT color EMPTY>
<!ATTLIST color value NMTOKEN #IMPLIED>
Schema:
<xs:element name="color">
  <xs:complexType>
    <!-- no content model is specified here -->
    <xs:attribute name="value" type="xs:NMTOKEN"/>
  </xs:complexType>
</xs:element>
Example 19-7. Any content
DTD:
<!ELEMENT anything ANY>
Schema:
<xs:element name="anything">
  <xs:complexType mixed="true">
    <xs:sequence>
      <xs:any minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
Example 19-8. Representing an enumerated attribute
DTD:
<!ATTLIST price currency (USD | CHF) "USD">
Schema:
<xs:attribute name="currency" default="USD">
  <xs:simpleType>
    <xs:restriction base="xs:token">
      <xs:enumeration value="USD"/>
      <xs:enumeration value="CHF"/>
    </xs:restriction>
  </xs:simpleType>
</xs:attribute>
Example 19-9. Representing a notation attribute
DTD:
<!ATTLIST picture fmt NOTATION (jpg | gif) "jpg">
Schema:
<xs:attribute name="fmt" default="jpg">
  <xs:simpleType>
    <xs:restriction base="xs:NOTATION">
      <xs:enumeration value="jpg"/>
      <xs:enumeration value="gif"/>
    </xs:restriction>
  </xs:simpleType>
</xs:attribute>
Example 19-10. Attribute declarations
DTD:
<!ATTLIST product
    id ID #REQUIRED
    name CDATA #IMPLIED
    type NMTOKEN "PR"
    version NMTOKEN #FIXED "A123">
Schema:
<xs:attribute name="id" type="xs:ID" use="required"/>
<xs:attribute name="name" type="xs:normalizedString"
              use="optional"/>
<xs:attribute name="type" type="xs:NMTOKEN" default="PR"/>
<xs:attribute name="version" type="xs:NMTOKEN" fixed="A123"/>
Example 19-11. Reusing entire content models
DTD:
<!ENTITY % AOrB "(a | b)">
<!ELEMENT x %AOrB;>
<!ELEMENT y %AOrB;>
Schema:
<xs:complexType name="AOrBType">
  <xs:choice>
    <xs:element ref="a"/>
    <xs:element ref="b"/>
  </xs:choice>
</xs:complexType>
<xs:element name="x" type="AOrBType"/>
<xs:element name="y" type="AOrBType"/>
Example 19-12. Reusing fragments of content models
DTD:
<!ENTITY % AOrB "a | b">
<!ELEMENT x ((%AOrB;), c)>
Schema:
<xs:group name="AOrBGroup">
  <xs:choice>
    <xs:element ref="a"/>
    <xs:element ref="b"/>
  </xs:choice>
</xs:group>
<xs:element name="x">
  <xs:complexType>
    <xs:sequence>
      <xs:group ref="AOrBGroup"/>
      <xs:element ref="c"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
Example 19-13. Reusing groups of attributes
DTD:
<!ENTITY % HeaderGroup "id ID #REQUIRED
                        variety NMTOKEN #IMPLIED">
<!ATTLIST x %HeaderGroup;>
Schema:
<xs:attributeGroup name="HeaderGroup">
  <xs:attribute name="id" type="xs:ID" use="required"/>
  <xs:attribute name="variety" type="xs:NMTOKEN"/>
</xs:attributeGroup>
<xs:element name="x">
  <xs:complexType>
    <xs:attributeGroup ref="HeaderGroup"/>
  </xs:complexType>
</xs:element>
Example 19-14. Allowing future extensions for sequence groups
DTD:
<!ENTITY % ext "" >
<!ELEMENT x (a, b %ext;)>
Schema:
<xs:group name="ext">
  <xs:sequence/>
</xs:group>
<xs:element name="x">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="a"/>
      <xs:element ref="b"/>
      <xs:group ref="ext"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
Example 19-15. Implementing extensions for sequence groups using redefine
DTD:
<!ENTITY % ext ", c, d" >
<!ENTITY % original SYSTEM "original.dtd">
%original;
Schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:redefine schemaLocation="original.xsd">
    <xs:group name="ext">
      <xs:sequence>
        <xs:group ref="ext"/>
        <xs:element ref="c"/>
        <xs:element ref="d"/>
      </xs:sequence>
    </xs:group>
  </xs:redefine>
</xs:schema>
Example 19-16. Implementing extensions for sequence groups using override
DTD:
<!ENTITY % ext ", c, d" >
<!ENTITY % original SYSTEM "original.dtd">
%original;
Schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:override schemaLocation="original.xsd">
    <xs:group name="ext">
      <xs:sequence>
        <xs:element ref="a"/>
        <xs:element ref="b"/>
        <xs:element ref="c"/>
        <xs:element ref="d"/>
      </xs:sequence>
    </xs:group>
  </xs:override>
</xs:schema>
Example 19-17. Allowing future extensions for choice groups
DTD:
<!ENTITY % ext "" >
<!ELEMENT x (a | b %ext;)*>
Schema:
<xs:element name="x">
  <xs:complexType>
    <xs:choice maxOccurs="unbounded">
      <xs:element ref="a"/>
      <xs:element ref="b"/>
      <xs:element ref="ext"/>
    </xs:choice>
  </xs:complexType>
</xs:element>
<xs:element name="ext" abstract="true" type="xs:string"/>
Example 19-18. Implementing extensions for choice groups
DTD:
<!ENTITY % ext "| c | d" >
<!ENTITY % original SYSTEM "original.dtd">
%original;
Schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:include schemaLocation="original.xsd"/>
  <xs:element name="c" substitutionGroup="ext"/>
  <xs:element name="d" substitutionGroup="ext"/>
</xs:schema>
Example 19-19. Allowing future extensions for attributes
DTD:
<!ENTITY % attExt "" >
<!ATTLIST x id ID #REQUIRED
        %attExt;>
Schema:
<xs:attributeGroup name="attExt"/>
<xs:element name="x">
  <xs:complexType>
    <!-- content model here -->
    <xs:attribute name="id" type="xs:ID" use="required"/>
    <xs:attributeGroup ref="attExt"/>
  </xs:complexType>
</xs:element>
Example 19-20. Implementing extensions for attributes using redefine
DTD:
<!ENTITY % attExt "myAttr NMTOKEN #IMPLIED" >
<!ENTITY % original SYSTEM "original.dtd">
%original;
Schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:redefine schemaLocation="original.xsd">
    <xs:attributeGroup name="attExt">
      <xs:attributeGroup ref="attExt"/>
      <xs:attribute name="myAttr" type="xs:NMTOKEN"/>
    </xs:attributeGroup>
  </xs:redefine>
</xs:schema>
Example 19-21. Implementing extensions for attributes using override
DTD:
<!ENTITY % attExt "myAttr NMTOKEN #IMPLIED" >
<!ENTITY % original SYSTEM "original.dtd">
%original;
Schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:override schemaLocation="original.xsd">
    <xs:attributeGroup name="attExt">
      <xs:attribute name="myAttr" type="xs:NMTOKEN"/>
    </xs:attributeGroup>
  </xs:override>
</xs:schema>
Example 19-22. Including other DTDs or schema documents
DTD:
<!ENTITY % prodInfo SYSTEM "prod.dtd">
%prodInfo;
Schema:
<xs:include schemaLocation="prod.xsd"/>
Example 19-23. Using a notation in an instance
<picture fmt="jpeg">47494638396132003200F7FF00FFFFFFFFFFCCFFFF99FF
FF66FFFF33FFFF00FF</picture>
Example 19-24. Declaring notations and notation attributes
<xs:notation name="jpeg" public="JPG"/>
<xs:notation name="gif" public="GIF"/>
<xs:simpleType name="PictureNotationType">
  <xs:restriction base="xs:NOTATION">
    <xs:enumeration value="jpeg"/>
    <xs:enumeration value="gif"/>
  </xs:restriction>
</xs:simpleType>
<xs:element name="picture">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:hexBinary">
        <xs:attribute name="fmt" type="PictureNotationType"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>
Example 19-25. A notation with an unparsed entity
Schema:
<xs:element name="picture">
  <xs:complexType>
    <xs:attribute name="location" type="xs:ENTITY"/>
  </xs:complexType>
</xs:element>
<!--...-->
Instance:
<!NOTATION jpeg SYSTEM "JPG">
<!ENTITY prod557 SYSTEM "prod557.jpg" NDATA jpeg>
<!ENTITY prod563 SYSTEM "prod563.jpg" NDATA jpeg>
]>
<catalog>
  <product>
    <number>557</number>
    <picture location="prod557"/>
  </product>
  <product>
    <number>563</number>
    <picture location="prod563"/>
  </product>
</catalog>
Example 19-26. Comments
DTD:
<!-- ******************** -->
<!-- CUSTOMER INFORMATION -->
<!-- ******************** -->
<!-- billing address -->
<!ELEMENT billTo (%AddressType;)>
<!-- shipping address -->
<!ELEMENT shipTo (%AddressType;)>
Schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:doc="http://datypic.com/doc">
  <xs:annotation>
    <xs:documentation>
      <doc:section>CUSTOMER INFORMATION</doc:section>
    </xs:documentation>
  </xs:annotation>
  <xs:element name="billTo" type="AddressType">
    <xs:annotation>
      <xs:documentation>
        <doc:description>billing address</doc:description>
      </xs:documentation>
    </xs:annotation>
  </xs:element>
  <xs:element name="shipTo" type="AddressType">
    <xs:annotation>
      <xs:documentation>
        <doc:description>shipping address</doc:description>
      </xs:documentation>
    </xs:annotation>
  </xs:element>
</xs:schema>
Example 19-27. 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>
Datypic XML Schema Services