Home arrow XML arrow Page 3 - The Fundamentals of DTD Design

Rainy Days - XML

Ever tried to read a DTD, and failed miserably? Ever wondered what all those symbols and weird language constructs meant? Well, fear not - this crash course will get you up to speed with the basics of DTD design in a hurry.

TABLE OF CONTENTS:
  1. The Fundamentals of DTD Design
  2. DTD Who?
  3. Rainy Days
  4. Simply Elementary
  5. What's The Frequency, Bobby?
  6. Turning Up The Heat
  7. An Entity In The Attic
  8. The Old Popcorn Trick
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 6
September 27, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Once you have an XML document and a DTD linked together, an XML parser can verify the document against the DTD and let you know if it finds errors. A number of tools are available online to perform this validation - my favourite is the XML Spy editor, available at http://www.xmlspy.com/ , although you can also try out expat, at http://sourceforge.net/projects/expat/, and rxp, at http://www.cogsci.ed.ac.uk/~richard/rxp.html

Here's what rxp has to say when I run it on the XML document above.

$ rxp -V weather.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE weather SYSTEM "weather.dtd"> <weather> <city>New York</city> <high>26</high> <low>18</low> <forecast>rain</forecast> </weather>
In other words - no error.

Let's suppose I altered the XML document instance a little, by modifying one of the element names and adding a new element.

<?xml version="1.0"?> <!DOCTYPE weather SYSTEM "weather.dtd"> <weather> <city>New York</city> <high>26</high> <low>18</low> <weekly_mean>21</weekly_mean> <daily_forecast>rain</daily_forecast> </weather>
While this version of the document is still well-formed, it no longer follows the rules laid down in "weather.dtd" and hence cannot be considered valid - which is why rxp barfs and generates a list of errors.

$ rxp -V weather.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE weather SYSTEM "weather.dtd"> <weather> <city>New York</city> <high>26</high> <low>18</low> Warning: Start tag for undeclared element weekly_mean in unnamed entity at line 7 char 14 of weather.xml Warning: Content model for weather does not allow element weekly_mean here in unnamed entity at line 7 char 14 of weather.xml <weekly_mean>21</weekly_mean> Warning: Start tag for undeclared element daily_forecast in unnamed entity at line 8 char 17 of weather.xml <daily_forecast>rain</daily_forecast> </weather>
Incidentally, it's also possible to place the DTD within the XML document itself. Although this is quite rare - the DTD is usually stored in a central place so that it can be referenced by different XML documents - you should know how to do it in case you're ever home on a Saturday evening and feel like experimenting.

<?xml version="1.0"?> <!DOCTYPE weather [ <!ELEMENT weather (city, high, low, forecast)> <!ELEMENT city (#PCDATA)> <!ELEMENT high (#PCDATA)> <!ELEMENT low (#PCDATA)> <!ELEMENT forecast (#PCDATA)> ]> <weather> <city>New York</city> <high>26</high> <low>18</low> <forecast>rain</forecast> </weather>


 
 
>>> More XML Articles          >>> More By Vikram Vaswani, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

XML ARTICLES

- Google Docs and Xpath Data Functions
- Flex Array Collection Sort and Filtering
- The Flex Tree Control
- Flex List Controls
- Working with Flex and Datagrids
- How to Set Up Podcasting and Vodcasting
- Creating an RSS Reader Application
- Building an RSS File
- An Introduction to XUL Part 6
- An Introduction to XUL Part 5
- An Introduction to XUL Part 4
- An Introduction to XUL Part 3
- An Introduction to XUL Part 2
- An Introduction to XUL Part 1
- XML Matters: Practical XML Data Design and M...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: