MSXML, concluded - The ValidateDocument() Function
(Page 3 of 5 )
You use the ValidateDocument() function to validate an XML document against the document’s DTD to determine if all elements in the XML document are defined in the DTD. Here’s the ValidateDocument() function. Notice that this is one of the simplest functions that you can build. It simply calls the validate() method and then evaluates the return value. If the returned errorCode is zero, then the XML document is valid. If the errorCode is other than zero, then the XML doesn’t comply with the DTD.
function ValidateDocument()
{
var err = objXML.validate();
if (err.errorCode == 0)
{
alert("Document is valid.");
}
else
{
alert("Error validating document:" + err.reason);
}
}
To test this function, return to the InsertFirst() function at the beginning of this chapter. Change the value of the new CD element in the text area of the HTML page to the following. Notice that the price element is deleted. This is required by the DTD.
<cd upc="75596280822">
<artist>Phish</artist>
<title>Live Phish, Vol. 15</title>
<label>ELEKTRA/WEA</label>
<date>2002-10-29</date>
</cd>
Click the InsertFirst() hyperlink and the XML document will look like this. Notice that price is missing, making the XML document invalid according to the DTD.
<?xml version="1.0"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd"> <catalog>
<cd upc="75596280822">
<artist>Phish</artist>
<title>Live Phish, Vol. 15</title>
<label>ELEKTRA/WEA</label>
<date>2002-10-29</date>
</cd>
<cd upc="602498678299">
<artist>U2</artist>
<title>How to Dismantle an Atomic Bomb</title>
<price>13.98</price>
<label>Interscope Records</label>
<date>2004-11-23</date>
</cd>
<cd upc="75679244222">
<artist>Led Zeppelin</artist>
<title>Physical Graffiti</title>
<price>22.99</price>
<label>Atlantic</label>
<date>1994-08-16</date>
</cd>
<cd upc="75678367229">
<artist>Rush</artist>
<title>Rush in Rio</title>
<price>13.98</price>
<label>Atlantic</label>
<date>2003-10-21</date>
</cd>
<cd upc="74646938720">
<artist>Billy Joel</artist>
<title>Songs in the Attic</title>
<price>10.99</price>
<label>Sony</label>
<date>1998-10-20</date>
</cd>
<cd upc="75678263927">
<artist>Led Zeppelin</artist>
<title>Houses of the Holy</title>
<price>10.98</price>
<label>Atlantic</label>
<date>1994-07-19</date>
</cd>
<cd upc="8811160227">
<artist>Jimi Hendrix</artist>
<title>Are You Experienced?</title>
<price>12.99</price>
<label>Experience Hendrix</label>
<date>1997-04-22</date>
</cd>
<cd upc="74640890529">
<artist>Bob Dylan</artist>
<title>The Times They Are A-Changin'</title>
<price>9.99</price>
<label>Sony</label>
<date>1990-10-25</date>
</cd>
</catalog>
The DOMDocument object doesn’t automatically revalidate the XML document each time it’s altered, so no error message is displayed. Now select the Validate Document link on the HTML page. The ValidateDocument() function validates the XML document and displays an alert message indicating that the XML Document is invalid. The alert message is something like:
Error validating document: Element content is invalid according to the DTD/Schema. Expecting: price.
This is telling you that the price element was expected.
Next: MSXML and XSLT >>
More XML Articles
More By McGraw-Hill/Osborne
|
This article is excerpted from chapter 10 of XML DeMYSTified, written by Jim Keogh and Ken Davidson (McGraw-Hill/Osborne, 2005; ISBN: 0072262109). Check it out today at your favorite bookstore. Buy this book now.
|
|