Pages

DTD for XML

We can define the content of an XML in a DTD (Document Type Definition). The DTD could be embedded in the XML file or, more commonly, placed in a stand alone file.

Here is an example of DTD:

<!ELEMENT name (first, middle, last)>
<!ELEMENT first (#PCDATA)>
<!ELEMENT middle (#PCDATA)>
<!ELEMENT last (#PCDATA)>

We are saying that the XML should consist of a root element named "name" that should include three elements, "first", "middle", "last", each of them a string of characters.

Say that this DTD is stored in a file named example.dtd, and we want example.xml, in the same folder, to use it. We'll write that xml in this way:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE name PUBLIC "- //ThisThread//First DTD//EN" "example.dtd">
<name>
<first>John</first>
<middle>Fearless</middle>
<last>Smith</last>
</name>

The doctype tag specifies in the XML the root element name and how to get the DTD. We could provide an id to the DTD using a Formal Public Identifier (FPI) in this format:
-//Owner//Class Description//Language//Version
After the FPI we specify the file name where the DTD is stored.

If you need more information on XML, a good book is Beginning XML by David Hunter et al (Wrox). I'm reading it while writing this post.

No comments:

Post a Comment