Stage Four

The XML and the DTD

We now have the following XML document:


<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="example.xsl"?>
<!DOCTYPE EXAMPLE SYSTEM "example.dtd">
<EXAMPLE>
<PASSWORD />
<TEXTINPUT>
 <NAME>myinput</NAME>
 <SIZE>20</SIZE>
</TEXTINPUT>
</EXAMPLE>

Small isn't it! This single XML file will create a fully operational form with a text input and a password input. To create the DTD we need to examine the structure of this file - the basis of all other XML files that will share this XSL and DTD. Everything is enclosed within the <EXAMPLE> tag. This is also specified as the name of the DTD (third line.) So EXAMPLE is our base. Within this, there is the <PASSWORD> tag - it contains no other tags. Next, there is the <TEXTINPUT> tag which completely surrounds the <NAME> and <SIZE> tags. This nesting should be familiar from HTML but it is much more strict in XML.

To translate this into a DTD, we use EXAMPLE as the base tag, specifying that it contains other tags, namely PASSWORD and TEXTINPUT. (NAME and SIZE are included within TEXTINPUT and will be dealt with separately.) This is done in the first line of the DTD:

<!ELEMENT EXAMPLE (PASSWORD*,TEXTINPUT+)>

The * denotes that PASSWORD is optional - it can occur once or more or not at all. If the * was omitted, every XML document would need one (and precisely only one) password control before the XML could be viewed. The + is slightly different - it denotes that TEXTINPUT must occur at least once in all XML documents using this DTD but it can occur more than once. It must not be omitted.

<!ELEMENT TEXTINPUT (NAME,SIZE)>

This defines the TEXTINPUT field as enclosing exactly one NAME tag and exactly one SIZE tag. Neither tag can be repeated or omitted. You may wish to make SIZE optional to allow the browser to assign a default value. NAME is required if the Perl script is to make any sense of the data.

<!ELEMENT NAME (#PCDATA)>
<!ELEMENT SIZE (#PCDATA)>
<!ELEMENT PASSWORD (#PCDATA)>

Every tag must be defined, either as enclosing another tag or in terms of the data it can contain. #PCDATA represent Parsed Character data - text that does not contain any XML reserved characters like < ! > & etc. The DTD is now complete.



This is part of www.codehelp.co.uk Copyright © 1998-2004 Neil Williams
See the file about.html for copying conditions.