Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0130655678

1st edition, , Prentice Hall PTR.

Chapter 19: Topics for DTD users

Book examples

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