[ Team LiB ] Previous Section Next Section

A Few Simple Rules for XML

You already know that an XML page must start with the <?xml?> tag and must contain a single root element. A few additional rules dictate how you create an XML page.

First, when you create an XML page, you have the option of specifying a Document Type Definition (DTD) that defines what elements and other components are permitted in the XML page. DTDs let you create standard definitions of XML pages. You can use an XML validator to check an XML page to make sure it conforms to its DTD. Use the <!DOCTYPE> tag to specify the DTD for your page. Listing 19.5 shows an example DTD.

Listing 19.5 Source Code for Simple.dtd
<!ELEMENT phone-book (entry*)>

!ELEMENT entry (person? | number*)+>
<!ELEMENT person (first-name? | middle-name? | last-name?)+>
<!ELEMENT first-name (#PCDATA)>
<!ELEMENT middle-name (#PCDATA)>
<!ELEMENT last-name (#PCDATA)>

Creating a DTD is more complex than creating an XML page. You should consult the W3C Web site at http://www.w3c.org for pointers on how to create a DTD. Listing 19.6 shows an XML page that conforms to the DTD in Listing 19.5.

Listing 19.6 Source Code for Simple.xml
<?xml version="1.0"?>
<!DOCTYPE Simple SYSTEM "http://localhost/Simple.dtd">

A DTD is optional, of course. You can create an XML page with any set of tags you want. For each opening tag you must have a closing tag. The exception to this rule is that you can end an opening tag with /> to indicate that it doesn't need a closing tag. Remember the <jsp:include/> and <jsp:forward/> tags end with /> and thus take no closing tag. You can't interleave tags, either. In other words, if tag B starts within tag A, then tag B must be closed before tag A closes. The following combination of tags is illegal in XML:


Because the <bar> tag starts inside the <foo> tag, it must also close within the <foo> tag.

XML now supports an alternative to DTDs called XMLSchema. An XML schema is similar to a DTD but includes more detail, including specific information about the type of data being stored. More importantly, an XML schema is itself a valid XML document, so it is easy to parse XML schemas.

You can also specify attributes within a tag. Although HTML is lenient about quotes in attributes, XML requires that the value of every attribute be enclosed in quotes. In other words, you might have an HTML tag like this:

<img src="katy.jpg" width=140 height=150>

A valid XML version of the <IMG> tag would look like this:

<img src="katy.jpg" width="140" height="150"/>

Notice that the XML version of <IMG> ends with /> because it doesn't need a closing tag.

    [ Team LiB ] Previous Section Next Section