Sams Teach Yourself XML in 21 Days

Sams Teach Yourself XML in 21 Days

By Steven Holzner

Using XML Validators

An XML validator checks your XML to make sure it's well formed and valid, giving you feedback if there's a problem. Here's a starter list of some validators on the Internet—note that your XML document must be online to use any of these validators, except for the Scholarly Technology Group validator, which can upload your XML document from your hard disk:

You can see the Scholarly Technology Group's validator at work in Figure 2.6. To give it something to chew on, we'll send it the XML document from yesterday's work, where we've exchanged the <message> and </heading> tags like this:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="ch01_04.css"?>
<!DOCTYPE document [
    <!ELEMENT document (heading, message)>
    <!ELEMENT heading (#PCDATA)>
    <!ELEMENT message (#PCDATA)>
]>
<document>
    <heading>
        Hello From XML
    <message>

       </heading>
        This is an XML document!
    </message>
</document>
02fig06.jpg

Figure 2.6 Using an XML validator.

Note also that to let the validator actually validate the document, you have to let it know what the document's syntax is. To do that, we've included a DTD in this document specifying that syntax (DTDs are coming up in Day 4, "Creating Valid XML Documents: Document Type Definitions," and Day 5, "Handling Attributes and Entities in DTDs."

When you click the Validate button in the validator, you get the results that appear in Figure 2.7. As you see in the figure, the validator indicates that these two tags are indeed swapped, causing a validation error.

02fig07.jpg

Figure 2.7 The results from an XML validator.

There's a built-in validator for documents with XML schema in Microsoft's Visual Studio .NET, and Internet Explorer can also validate documents with XML schemas. (As you'll see in more depth in Day 6, "Creating Valid XML Documents: XML Schemas," Visual Studio lets you generate an XML schema for an XML document with the XML, Create Schema menu item.) You can use the XML, Validate XML Data menu item to validate an XML document that uses a schema, as you see in Figure 2.8. Note the text No validation errors were found at the lower left, which is Visual Studio .NET's subtle way of telling you that the document is OK.

02fig08.jpg

Figure 2.8 XML validation in Visual Studio .NET.

That gives you a good overview of the kinds of tools available to help you develop your XML these days—editors, browsers, and validators. Now it's time to get down to brass tacks and understand what makes an XML document tick, piece by piece.

Share ThisShare This

Informit Network