DTD & Validity Examples

date1.xml
<?xml version="1.0"?>
<date>
 10/12/2001
</date>

 

date1.xml with internal DTD
<?xml version="1.0" standalone="no"?>
<!DOCTYPE date [
 <!ELEMENT date (#PCDATA)>
]>
<date>
 10/12/2001
</date>

 

date2.xml with external DTD declaration
<?xml version="1.0" standalone="no"?>
<!DOCTYPE date SYSTEM "date2.dtd">

<date>
 <month>10</month>
 <day>12</day>
 <year>2001</year>
</date>

 

date2.dtd
<!ELEMENT date (month, day, year)>
<!ELEMENT month (#PCDATA)>
<!ELEMENT day (#PCDATA)>
<!ELEMENT year (#PCDATA)>

 

invalid XML
<?xml version="1.0"? standalone="no">
<!DOCTYPE date SYSTEM "date2.dtd">

<date>
 <month>
  <full_name>October</full_name>
  <value>10</value>
 </month>
 <day>12</day>
 <year>2001</year>
</date>

 

DTD Element Declarations Examples
Basic Template
<!ELEMENT element_name (element_content)> Using Parsed Character Data <!ELEMENT month (#PCDATA)> ANY Content Model <!ELEMENT month ANY> Sequence delimited by commas <!ELEMENT date (month, day, year)> Cardinality Operators +, *, & ? Zero or one children ? <!ELEMENT date (month, day?, year)> One or more children + <!ELEMENT date (month+, day, year)> Zero or more children * <!ELEMENT date (month, day, year*)> Choices <!ELEMENT date ((month_num | month_name), day, year)> Empty Elements <!ELEMENT br EMPTY>

 

DTD Attribute Declarations Examples
Basic Template
<!ATTLIST element attribute type default_value> if I wanted to assign the element date the optional attribute id, use: <!ATTLIST date id CDATA> Making the attribute required <!ATTLIST date id CDATA #REQUIRED> <!ATTLIST date id CDATA #IMPLIED> <!ATTLIST date id CDATA #FIXED "3f">

 

slave.xml
<?xml version="1.0" standalone="no"?>
<!DOCTYPE laslave [

<!ELEMENT laslave (family)+>

<!ELEMENT family (mother, father, child*)>
<!ELEMENT mother EMPTY>
<!ELEMENT father EMPTY>
<!ELEMENT child EMPTY>
<!ATTLIST mother id IDREF #REQUIRED>
<!ATTLIST father id IDREF #REQUIRED>
<!ATTLIST child id IDREF #REQUIRED>

<!ELEMENT slave ANY>
<!ATTLIST slave id ID #REQUIRED>

<!ELEMENT name (#PCDATA)>

]>

<laslave>
 <family>
  <mother id="29345" />
  <father id="49219" />
  <child id="83920" />
 </family>

 <slave id="29345">
  <name>Sylvia</name>
 </slave>
 <slave id="49219">
  <name>John</name>
 </slave>
 <slave id="83920">
<name>Harry</name>
</slave> </laslave>

 

book.xml
<?xml version="1.0" standalone="no"?>
<repository>

 <book isbn="1-903450-31-4">
  <title>Foundation Flash 5</title>
  <author>
   <firstname>Sham</firstname>
   <surname>Bhangal</surname>
  </author>
  <author>
   <firstname>Amanda</firstname>
   <surname>Farr</surname>
  </author>
  <author>
   <firstname>Patrick</firstname>
   <surname>Rey</surname>
  </author>
  <publisher>
   <label>friends of ED</label>
   <address>
    <street>30 Lincoln Road</street>
    <street>Olton</street>
    <city>Olton</city>
    <province>Birmingham</province>
    <zip>B27 6PA</zip>
    <country>UK</country>
   </address>
   <editor type="main">
    <firstname>Andy</firstname>
    <surname>Corsham</surname>
   </editor>
   <editor type="graphic">
    <firstname>William</firstname>
    <surname>Fallon</surname>
   </editor>
  </publisher>
  <copyright year="2000" />
 </book>

 <book isbn="0-385-26570-0">
  <title>The Vintage Bradbury</title>
  <author>
   <firstname>Ray</firstname>
   <surname>Bradbury</surname>
  </author>
  <publisher>
   <label>Random House, Inc.</label>
   <division>Vintage Books</division>
   <address>
    <city>New York</city>
    <country>US</country>
   </address>
  </publisher>
  <contributor>
   <firstname>Gilber</firstname>
   <surname>Highet</surname>
  </contributor>
  <copyright year="1965" />
 </book>

 <book isbn="0-451-19741-0">
  <title>How Stella Got Her Groove Back</title>
  <author>
   <firstname>Terry</firstname>
   <surname>McMillan</surname>
  </author>
  <publisher>
   <label>Penguin Group</label>
   <division>SIGNET</division>
   <address>
    <street>375 Hudson Street</street>
    <city>New York</city>
    <state>New York</state>
    <zip>10014</zip>
    <country>US</country>
   </address>
  </publisher>
  <copyright year="1996" />
 </book>

</repository>

 

book.dtd
<!--The root element-->
<!ELEMENT repository (book)+>



<!ELEMENT book (title, author+, publisher, contributor*,
  copyright)>


<!ATTLIST book isbn ID #REQUIRED>


<!ELEMENT title (#PCDATA)>


<!ELEMENT author (firstname, surname, email*)>


<!ELEMENT firstname (#PCDATA)>


<!ELEMENT surname (#PCDATA)>


<!ELEMENT email (#PCDATA)>


<!ELEMENT publisher (label, division?, address)>


<!ELEMENT label (#PCDATA)>


<!ELEMENT division (#PCDATA)>


<!ELEMENT address (street?, city?, state?, province?,
  zip?, country)>


<!ELEMENT street (#PCDATA)>


<!ELEMENT city (#PCDATA)>


<!ELEMENT state (#PCDATA)>


<!ELEMENT province (#PCDATA)>


<!ELEMENT zip (#PCDATA)>


<!ELEMENT country (#PCDATA)>


<!ELEMENT copyright (#PCDATA)>

 

anybook.dtd
<!--The root element-->
<!ELEMENT repository (book+)>

<!ELEMENT book ANY>