Java & J2EE Page 7 - Integrating XML with J2EE |
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
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. Defining Attributes 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
The ATTLIST default-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
Example DTD 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. Listing 16.7 DTD for jobSummary XML <!ELEMENT jobSummary (job*)> <!ELEMENT job (location, description, skill+)> <!ATTLIST job customer CDATA #REQUIRED> <!ATTLIST job reference CDATA #REQUIRED> <!ELEMENT location (#PCDATA)> <!ELEMENT description (#PCDATA)> <!ELEMENT skill (#PCDATA)> 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").
blog comments powered by Disqus |
|
|
|
|
|
|
|