In this article, Martin Bond discusses XML and its associated APIs and standards, and how XML can be used to create flexible structured data that is inherently portable. This excerpt is from chapter (Day) 16 of Teach Yourself J2EE in 21 Days, second edition, by Martin Bond, et. al. (Sams, ISBN: 0672325586)
The DTD defines every element in the XML document with element type declarations. Each element type declaration takes the following form:
<!ELEMENT name ( content ) >
For example, for the jobSummary XML document in Listing 16.4, the jobSummary root element is defined as
<!ELEMENT jobSummary ( job* )>
The * sign indicates that the jobSummary element may consist of zero or more job elements. There are other symbols used to designate rules for combining elements and these are listed in Table 16.3.
Table 16.3 Occurrence Characters Used in DTD Definitions
Zero or more (not required)
One or more (at least one required)
Element is optional (if present can only appear once)
Group of elements
The following defines an XML job element that must include one location, an optional description, and at least one skill:
<!ELEMENT job (location, description?, skill+)>
Defining the Element Content
Elements can contain other elements, or content, or have elements and content. The jobSummary element, in Listing 16.4, contains other elements but no text body; whereas the location element has a text body but does not contain any elements.
To define an element that has a text body, use the reference #PCDATA (Parsed Character DATA). For example, the location element in Listing 16.4 is defined by
<!ELEMENT location (#PCDATA)>
An element can also have no content (the <br> tag in HTML is such an example). This tag would be defined with the EMPTY keyword as
<!ELEMENT br EMPTY>
You will also see elements defined with contents of ANY. The ANY keyword denotes that the element can contain all possible elements, as well as PCDATA. The use of ANY should be avoided. If your data is so unstructured that it cannot be defined explicitly, there probably is no point in creating a DTD in the first place.
In Listing 16.4, the job element has two attributes—customer and reference. Attributes are defined in an ATTLIST that has the following form:
<!ATTLIST element attribute type default-value>
The element is the name of the element and attribute is the name of the attribute. The type defines the kind of attribute that is expected. A type is either one of the defined constants described in Table 16.4, or it is an enumerated type where the permitted values are given in a bracketed list.
Table 16.4 DTD Attribute Types
Attribute Is a...
Valid XML name.
Multiple XML names.
An element found elsewhere in the document. The value for IDREF must match the ID of another element.
External binary data file (such as a gif image).
Multiple external binary files.
The ATTLISTdefault-value component defines a value that will be used if one is not supplied. For example
<!ATTLIST button visible (true | false) "true").
defines that the element button has an attribute called visible that can be either true or false. If the attribute is not supplied, because a default value is supplied, it will be set to be true.
The default-value item can also be used to specify that the attribute is #REQUIRED, #FIXED, or #IMPLIED. The meaning of these values is given in Table 16.5.
Table 16.5 DTD Attribute Default Values
Attribute must be provided.
Effectively a constant declaration. The attribute must be set to the given value or the XML is not valid.
The attribute is optional and the processing application is allowed to use any appropriate value if required.
Listing 16.7 is the DTD for the jobSummary XML document. Create the DTD in a file called jobSummary.dtd in the same directory as your jobSummary XML document.
Don't forget to add the following line to the jobSummary XML at line 2 (following the PI):
<!DOCTYPE jobSummary SYSTEM "jobSummary.dtd">
View the jobSummary.xml document in your XML browser or other XML validator.
If the browser cannot find the DTD, it will generate an error. Edit jobSummary.xml, remove the customer attribute, and check that your XML validator generates an appropriate error (such as "Required attribute 'customer' is missing").
This chapter is from Teach Yourself J2EE in 21 Days, second edition, by Martin Bond et. al. (Sams, 2004, ISBN: 0-672-32558-6). Check it out at your favorite bookstore today. Buy this book now.