Parsing a Parameterized DTD
This section uses the Echo program to see what happens when you reference
xhtml.dtd
inslideshow2.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 referencesslideshow2.dtd
. The output is contained inEcho10-08.txt
. (The browsable versions areslideSample08-xml.html
,slideshow2-dtd.html
, andEcho10-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 atitle
element that is entirely different from thetitle
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 aslideshow
"namespace" that you artificially constructed by hyphenating the name, so thetitle
element in the "slideshow
namespace" (slide-title
, really) is no longer in conflict with thetitle
element inxhtml.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:
- Providing additional declarations for entities, attributes, or notations. (Such declarations are ignored. Only the first is used. Also, note that duplicate definitions of elements always produce a fatal error when validating, as you saw earlier.)
- Referencing an undeclared element type. (A validity error occurs only if the undeclared type is actually used in the XML document. A warning results when the undeclared element is referenced in the DTD.)
- Declaring attributes for undeclared element types.
The Java XML SAX parser also emits warnings in other cases:
- No
<!DOCTYPE ...>
when validating.- References to an undefined parameter entity when not validating. (When validating, an error results. Although nonvalidating parsers are not required to read parameter entities, the Java XML parser does so. Because it is not a requirement, the Java XML parser generates a warning, rather than an error.)
- Certain cases where the character-encoding declaration does not look right.
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.