Parsing a Parameterized DTD

This section uses the Echo program to see what happens when you reference xhtml.dtd in slideshow2.dtd. It also covers the kinds of warnings that are generated by the SAX parser when a DTD is present.

Note: The XML file used here is slideSample08.xml, which references slideshow2.dtd. The output is contained in Echo10-08.txt. (The browsable versions are slideSample08-xml.html, slideshow2-dtd.html, and Echo10-08.html.)

When you try to echo the slide presentation, you will find that it now contains a new error. The relevant part of the output is shown here (formatted for readability):

<?xml version='1.0' encoding='UTF-8'?>
** Parsing error, line 22, uri: .../slideshow.dtd
Element type "title" must not be declared more than once. 

Note: The foregoing message was generated by the JAXP 1.2 libraries. If you are using a different parser, the error message is likely to be somewhat different.

The problem is that xhtml.dtd defines a title element that is entirely different from the title element defined in the slideshow DTD. Because there is no hierarchy in the DTD, these two definitions conflict.

The slideSample09.xml version solves the problem by changing the name of the slide title. Run the Echo program on that version of the slide presentation. It should run to completion and display output like that shown in Echo10-09.

Congratulations! You have now read a fully validated XML document. The change in that version of the file has the effect of putting the DTD's title element into a slideshow "namespace" that you artificially constructed by hyphenating the name, so the title element in the "slideshow namespace" (slide-title, really) is no longer in conflict with the title element in xhtml.dtd.

Note: As mentioned in Using Namespaces, namespaces let you accomplish the same goal without having to rename any elements.

Next, we'll take a look at the kinds of warnings that the validating parser can produce when processing the DTD.

DTD Warnings

As mentioned earlier, warnings are generated only when the SAX parser is processing a DTD. Some warnings are generated only by the validating parser. The nonvalidating parser's main goal is operate as rapidly as possible, but it too generates some warnings. (The explanations that follow tell which does what.)

The XML specification suggests that warnings should be generated as a result of the following:

The Java XML SAX parser also emits warnings in other cases:

At this point, you have digested many XML concepts, including DTDs and external entities. You have also learned your way around the SAX parser. The remainder of this chapter covers advanced topics that you will need to understand only if you are writing SAX-based applications. If your primary goal is to write DOM-based applications, you can skip ahead to Chapter 6.