Definitive XML Schema
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 0132886723
2nd edition, , Prentice Hall PTR.
Chapter 19: Topics for DTD users
Book examples
<!ELEMENT price (#PCDATA)>
<xs:element name="price" type="xs:decimal"/>
<!ELEMENT price (#PCDATA)> <!ATTLIST price currency NMTOKEN #IMPLIED>
<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>
<!ELEMENT product (number, name+, size?, color*)>
<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>
<!ELEMENT el ((a | b)*, (c | d)?)>
<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>
<!ELEMENT letter (#PCDATA | custName | prodName)*>
<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>
<!ELEMENT color EMPTY> <!ATTLIST color value NMTOKEN #IMPLIED>
<xs:element name="color"> <xs:complexType> <!-- no content model is specified here --> <xs:attribute name="value" type="xs:NMTOKEN"/> </xs:complexType> </xs:element>
<!ELEMENT anything ANY>
<xs:element name="anything"> <xs:complexType mixed="true"> <xs:sequence> <xs:any minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element>
<!ATTLIST price currency (USD | CHF) "USD">
<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>
<!ATTLIST picture fmt NOTATION (jpg | gif) "jpg">
<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>
<!ATTLIST product id ID #REQUIRED name CDATA #IMPLIED type NMTOKEN "PR" version NMTOKEN #FIXED "A123">
<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"/>
<!ENTITY % AOrB "(a | b)"> <!ELEMENT x %AOrB;> <!ELEMENT y %AOrB;>
<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"/>
<!ENTITY % AOrB "a | b"> <!ELEMENT x ((%AOrB;), c)>
<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>
<!ENTITY % HeaderGroup "id ID #REQUIRED variety NMTOKEN #IMPLIED"> <!ATTLIST x %HeaderGroup;>
<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>
<!ENTITY % ext "" > <!ELEMENT x (a, b %ext;)>
<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>
<!ENTITY % ext ", c, d" > <!ENTITY % original SYSTEM "original.dtd"> %original;
<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>
<!ENTITY % ext ", c, d" > <!ENTITY % original SYSTEM "original.dtd"> %original;
<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>
<!ENTITY % ext "" > <!ELEMENT x (a | b %ext;)*>
<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"/>
<!ENTITY % ext "| c | d" > <!ENTITY % original SYSTEM "original.dtd"> %original;
<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>
<!ENTITY % attExt "" > <!ATTLIST x id ID #REQUIRED %attExt;>
<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>
<!ENTITY % attExt "myAttr NMTOKEN #IMPLIED" > <!ENTITY % original SYSTEM "original.dtd"> %original;
<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>
<!ENTITY % attExt "myAttr NMTOKEN #IMPLIED" > <!ENTITY % original SYSTEM "original.dtd"> %original;
<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>
<!ENTITY % prodInfo SYSTEM "prod.dtd"> %prodInfo;
<xs:include schemaLocation="prod.xsd"/>
<picture fmt="jpeg">47494638396132003200F7FF00FFFFFFFFFFCCFFFF99FF FF66FFFF33FFFF00FF</picture>
<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>
<xs:element name="picture"> <xs:complexType> <xs:attribute name="location" type="xs:ENTITY"/> </xs:complexType> </xs:element> <!--...-->
<!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>
<!-- ******************** --> <!-- CUSTOMER INFORMATION --> <!-- ******************** --> <!-- billing address --> <!ELEMENT billTo (%AddressType;)> <!-- shipping address --> <!ELEMENT shipTo (%AddressType;)>
<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>
<!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>