ITworld.com
  Search  
Menu Changing the way you view IT
It's Nice to Share
Sign up for XML IN PRACTICE
More Newsletters
 
 

XML IN PRACTICE --- 12/07/2000



Mark Johnson

XML was designed so instances of XML (XML files, XML messages, etc...) can be "self-describing". They contain DTDs describing the file’s internal structure allowing the parser to chop the input stream up into identifiable pieces (identifiable via the DTD, that is), and then do useful things with those pieces.
Advertisement
On this topic




For example, here's a little XML document containing both a document- type declaration (everything inside the <!DOCTYPE>), and an XML document:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Artists [ <!-- This is a document type definition (DTD) for Artists --> <!ELEMENT Artists (Artist)*> <!ELEMENT Artist (Name,Born,Died)> <!ATTLIST Artist Continent (AF|AN|AS|AU|EU|NA|SA) #REQUIRED> <!ELEMENT Name (#PCDATA)> <!ELEMENT Born (#PCDATA)> <!ELEMENT Died (#PCDATA)> ]> <Artists> <Artist Continent="EU"> <Name>Van Gogh, Vincent</Name> <Born>1853</Born> <Died>1890</Died> </Artist> <Artist Continent="SA"> <Name>Kahlo, Frida</Name> <Born>1907</Born> <Died>1954</Died> </Artist> </Artists>

The document type declaration (<!DOCTYPE>) contains the document type definition. But what if you had a few thousand of these documents and you wanted to change the DTD? You'd have to edit every document to update the DTD.

For this reason, and many others, keeping DTDs separate from the documents they describe can be very useful. When many documents must conform to the same DTD, you can separate the DTD from the XML documents and share the single DTD between documents. If you need to change the DTD, you can change it in one place and all the files that use it will have a new DTD.

The trick uses the SYSTEM keyword in the document type declaration to define an external DTD. The resulting DTD chunk comes from a file called an "external subset" in SGML lingo.

Our example above can be separated into two files. First, there's the DTD file "Artists.dtd":

<?xml version="1.0" encoding="UTF-8"?>
<!-- This is a document type definition (DTD) for Artists --> <!ELEMENT Artists (Artist)*> <!ELEMENT Artist (Name,Born,Died)> <!ATTLIST Artist Continent (AF|AN|AS|AU|EU|NA|SA) #REQUIRED> <!ELEMENT Name (#PCDATA)> <!ELEMENT Born (#PCDATA)> <!ELEMENT Died (#PCDATA)>

And here's the resulting XML file, using the SYSTEM keyword to identify the external document type declaration:

<?xml version="1.0"?>
<!-- Here's the external DTD declaration: --> <!DOCTYPE Artists SYSTEM "Artists.dtd"> <!-- Here's the XML: --> <Artists> <Artist Continent="EU"> <Name>Van Gogh, Vincent</Name> <Born>1853</Born> <Died>1890</Died> </Artist> <Artist Continent="SA"> <Name>Kahlo, Frida</Name> <Born>1907</Born> <Died>1954</Died> </Artist> </Artists>

The <!DOCTYPE> line in the XML document above keeps your DTD in a separate document from the XML. In future newsletters, I'll explore additional reasons for separating the two, and show you some tricks for making DTDs more flexible and extensible.

 

Mark Johnson is president of Elucify Technical Communications, a Colorado-based training and consulting company dedicated to clarifying novel or complex ideas through clear explanation and examples.

Sponsored links
Locate Hidden Software on business PCs with this free tool
Top 5 Reasons to Combine App Performance and Security
KODAK i1400 Series Scanners stand up to the challenge
Bring harmony to your mix of UNIX-Linux-Windows computing environments
www.itworld.com    open.itworld.com     security.itworld.com     smallbusiness.itworld.com
storage.itworld.com     utilitycomputing.itworld.com     wireless.itworld.com

 
Contact Us   About Us   Privacy Policy    Terms of Service   Reprints  

CIO   Computerworld   CSO   GamePro   Games.net   IDG Connect   IDG World Expo   Industry Standard   Infoworld   ITworld   JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

Copyright © Computerworld, Inc. All rights reserved

Reproduction in whole or in part in any form or medium without express written permission of Computerworld Inc. is prohibited. Computerworld and Computerworld.com and the respective logos are trademarks of International Data Group Inc.