How do I write my own DTD?
Answer:
You need to use the XML Declaration Syntax (very simple: declaration keywords begin
with
<!ELEMENT Shopping-List (Item)+>
<!ELEMENT Item (#PCDATA)>
It says that there shall be an element called Shopping-List and that it shall contain
elements called Item: there must be at least one Item (that's the plus sign) but there may
be more than one. It also says that the Item element may contain only parsed character
data (PCDATA, ie text: no further markup).
Because there is no other element which contains Shopping-List, that element is assumed
to be the ‘root’ element, which encloses everything else in the document. You can now
use it to create an XML file: give your editor the declarations:
<?xml version="1.0"?>
<!DOCTYPE Shopping-List SYSTEM "shoplist.dtd">
(assuming you put the DTD in that file). Now your editor will let you create files
according to the pattern:
<Shopping-List>
<Item>Chocolate</Item>
<Item>Sugar</Item>
<Item>Butter</Item>
</Shopping-List>
It is possible to develop complex and powerful DTDs of great subtlety, but for any
significant use you should learn more about document systems analysis and document
type design. See for example Developing SGML DTDs: From Text to Model to Markup
(Maler and el Andaloussi, 1995): this was written for SGML but perhaps 95% of it
applies to XML as well, as XML is much simpler than full SGML—see the list of
restrictions which shows what has been cut out.
Warning
Incidentally, a DTD file never has a DOCTYPE Declaration in it: that only occurs in an
XML document instance (it's what references the DTD). And a DTD file also never has
an XML Declaration at the top either. Unfortunately there is still software around which
inserts one or both of these.
with
<!ELEMENT Shopping-List (Item)+>
<!ELEMENT Item (#PCDATA)>
It says that there shall be an element called Shopping-List and that it shall contain
elements called Item: there must be at least one Item (that's the plus sign) but there may
be more than one. It also says that the Item element may contain only parsed character
data (PCDATA, ie text: no further markup).
Because there is no other element which contains Shopping-List, that element is assumed
to be the ‘root’ element, which encloses everything else in the document. You can now
use it to create an XML file: give your editor the declarations:
<?xml version="1.0"?>
<!DOCTYPE Shopping-List SYSTEM "shoplist.dtd">
(assuming you put the DTD in that file). Now your editor will let you create files
according to the pattern:
<Shopping-List>
<Item>Chocolate</Item>
<Item>Sugar</Item>
<Item>Butter</Item>
</Shopping-List>
It is possible to develop complex and powerful DTDs of great subtlety, but for any
significant use you should learn more about document systems analysis and document
type design. See for example Developing SGML DTDs: From Text to Model to Markup
(Maler and el Andaloussi, 1995): this was written for SGML but perhaps 95% of it
applies to XML as well, as XML is much simpler than full SGML—see the list of
restrictions which shows what has been cut out.
Warning
Incidentally, a DTD file never has a DOCTYPE Declaration in it: that only occurs in an
XML document instance (it's what references the DTD). And a DTD file also never has
an XML Declaration at the top either. Unfortunately there is still software around which
inserts one or both of these.
No comments:
Post a Comment