Chapter 19: Topics for DTD users
Book Examples
Example 19-1. Simple type
<!ELEMENT price (#PCDATA)>
<xsd:element name="price" type="xsd:decimal"/>
Example 19-2. Simple content (with attributes)
<!ELEMENT price (#PCDATA)>
<!ATTLIST price currency NMTOKEN #IMPLIED>
<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
<!ELEMENT product (number, name+, size?, color*)>
<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
<!ELEMENT el ((a | b)*, (c | d)?)>
<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
<!ELEMENT letter (#PCDATA | custName | prodName)*>
<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
<!ELEMENT color EMPTY>
<!ATTLIST color value NMTOKEN #IMPLIED>
<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
<!ELEMENT anything ANY>
<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
<!ATTLIST price currency (USD | CHF) "USD">
<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
<!ATTLIST picture fmt NOTATION (jpg | gif) "jpg">
<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
<!ATTLIST product
id ID #REQUIRED
name CDATA #IMPLIED
type NMTOKEN "PR"
version NMTOKEN #FIXED "A123">
<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
<!NOTATION jpeg SYSTEM "JPG">
<xsd:notation name="jpeg" system="JPG"/>
Example 19-12. Reusing entire content models
<!ENTITY % AOrB "(a | b)">
<!ELEMENT x %AOrB;>
<!ELEMENT y %AOrB;>
<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
<!ENTITY % AOrB "a | b">
<!ELEMENT x ((%AOrB;) , c)>
<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
<!ENTITY % HeaderGroup "id ID #REQUIRED
variety NMTOKEN #IMPLIED">
<!ATTLIST x %HeaderGroup;>
<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
<!ENTITY % ext "" >
<!ELEMENT x (a , b %ext;)>
<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
<!ENTITY % ext ", c, d" >
<!ENTITY % original SYSTEM "original.dtd">
%original;
<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
<!ENTITY % ext "" >
<!ELEMENT x (a | b %ext;)*>
<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
<!ENTITY % ext "| c | d" >
<!ENTITY % original SYSTEM "original.dtd">
%original;
<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
<!ENTITY % attExt "" >
<!ATTLIST x id ID #REQUIRED
%attExt;>
<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
<!ENTITY % attExt "myAttr NMTOKEN #IMPLIED" >
<!ENTITY % original SYSTEM "original.dtd">
%original;
<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
<!ENTITY % attExt "" >
<!ENTITY % HeaderGroup "id ID #REQUIRED
variety NMTOKEN #IMPLIED
%attExt;">
<!ATTLIST x %HeaderGroup;>
<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
<!ENTITY % attExt "myAttr NMTOKEN #IMPLIED" >
<!ENTITY % original SYSTEM "original.dtd">
%original;
<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
<!ENTITY % prodInfo SYSTEM "prod.dtd">
%prodInfo;
<xsd:include schemaLocation="prod.xsd"/>
Example 19-24. Unparsed entity
<xsd:element name="product">
<xsd:complexType>
<xsd:attribute name="number" type="ProdNumType"/>
<xsd:attribute name="picture" type="xsd:ENTITY"/>
</xsd:complexType>
</xsd:element>
<!DOCTYPE catalog [
<!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
<!-- ******************** -->
<!-- CUSTOMER INFORMATION -->
<!-- ******************** -->
<!-- billing address -->
<!ELEMENT billTo (%AddressType;)>
<!-- shipping address -->
<!ELEMENT shipTo (%AddressType;)>
<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>
|