156 Part I: Core Ideas
$message = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<!DOCTYPE pollresults [
<!ELEMENT pollresults (rating,average,votes)>
<!ELEMENT rating (#PCDATA)>
<!ELEMENT average (#PCDATA)>
<!ELEMENT votes (#PCDATA)>
<!ATTLIST rating id ID #IMPLIED>
<!ATTLIST average id ID #IMPLIED>
<!ATTLIST votes id ID #IMPLIED>
]>
<pollresults>
<rating id=\"rating\">$rating</rating>
<average id=\"average\">$average</average>
<votes id=\"votes\">$votes</votes>
</pollresults>";
echo $message;
Of course, it is also possible to include the DTD as an external reference as well using
a line like:
<!DOCTYPE pollresults SYSTEM "ratings.dtd">
in the response. With document semantics defined, we must consider if browsers can do
anything with these rules.
Let’s get right to the problem and state an unfortunate situation, Firefox at this point of
writing is incapable of validating XML markup using a DTD. Interestingly though, as you will
see in a few moments, Firefox can consult an internally provided DTD that can be used to
solve the getElementById problem encountered by many developers. However, it cannot
in any fashion at this moment in time reference an external DTD.
Interestingly in comparison, Internet Explorer can, in some cases, validate XML content.
For example, the following script could be used to validate a response by loading the string
of an XML response packet into the DOM parser:
if (window.ActiveXObject)
{
doc= new ActiveXObject("MSXML2.DOMDocument.3.0");
doc.async="false";
doc.validateOnParse = true;
validated = doc.loadXML(xhr.responseText);
}
If the DTD is either internal or external, it will validate the provided markup returning
true or false. If it does not validate, you will not be given a DOM tree to play with. You
might desire to turn off validation, but that is a somewhat pointless setting as it is what the
responseXML property does by default anyway: it checks well-formedness.
Unfortunately, there is a catch to IE’s validating parser: it only works in some ActiveX
versions. If you use the later MSXML2.DOMDocument.6.0 instantiation, you cannot
validate XML. Supposedly for security reasons, you are not able to load an external DTD.
Unfortunately, it also seems that you cannot validate against an internal DTD either. Maybe
in future versions this will change, but for now in IE you are either forced to use an older
XML parser or simply avoid validation of XML markup at the browser.