Definitive XML Schema
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 0130655678
1st edition, , Prentice Hall PTR.
Chapter 19: Topics for DTD users
Book examples
<!ELEMENT price (#PCDATA)>
<xsd:element name="price" type="xsd:decimal"/>
<!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>
<!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>
<!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>
<!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>
<!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>
<!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>
<!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>
<!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>
<!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"/>
<!NOTATION jpeg SYSTEM "JPG">
<xsd:notation name="jpeg" system="JPG"/>
<!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"/>
<!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>
<!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>
<!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"/>
<!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>
<!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"/>
<!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>
<!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"/>
<!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>
<!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>
<!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>
<!ENTITY % prodInfo SYSTEM "prod.dtd"> %prodInfo;
<xsd:include schemaLocation="prod.xsd"/>
<xsd:element name="product"> <xsd:complexType> <xsd:attribute name="number" type="ProdNumType"/> <xsd:attribute name="picture" type="xsd:ENTITY"/> </xsd:complexType> </xsd:element>
<!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>
<!-- ******************** --> <!-- 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>