Validation Tools

It's good to develop an eye for clean XML code, which makes it possible to develop XML documents with a minimal amount of errors. However, it's difficult for any human to perform such a technical task flawlessly, which is where XML validation tools come into play. XML validation tools are used to analyze the contents of XML documents to make sure they conform to a schema. There are two main types of validation tools available:

  • Web-based tools

  • Standalone tools

Web-based tools are web pages that allow you to enter the path (URI) of an XML document to have it validated. The upside to web-based tools is that they can be used without installing special softwarejust open the web page in a web browser and go for it! The downside to web-based validation tools is that they sometimes don't work well when you aren't dealing with files that are publicly available on the Internet. For example, if you're working on an XML document on your local hard drive, it can be tough getting a web-based validation tool to work properly if the schema is also stored locally. Typically it's a matter of getting the tool to recognize the schema; if the schema is located on the Internet there usually isn't a problem, but if it's located on your local hard drive, it can be tough getting things to work properly.

If you're planning to do a lot of XML development work on your local hard drive, you might want to consider using a standalone validation tool. Standalone validation tools are tools that you must install on your computer in order to use. These kinds of tools range from full-blown XML editors such as XML Spy to command-line XML validators such as the W3C's XSV validator. Standalone validation tools have the benefit of allowing you to validate local files with ease. The drawback to these tools is that some of them aren't cheap, and they must be installed on your computer. However, if you don't mind spending a little money, a standalone tool can come in extremely handy.

For the record, not all standalone tools cost money. For example, the W3C's XSV validator is available for free download at http://www.ltg.ed.ac.uk/~ht/xsv-status.html. XMLStarlet is another good option. It is an open source command-line XML tool that is freely available to download at http://xmlstar.sourceforge.net/.

Regardless of what type of validation tool you decide to use, there is a big distinction between validating documents against DTDs and validating them against XSDs. Although some tools support both types of schemas, many tools do not. You should therefore consider what type of schema you plan on using when assessing the different tools out there.

DTD Validation

DTDs have been around much longer than XSDs, so you'll find that there are many more validation tools available for DTDs. One of the best tools I've found is made available by Brown University's Scholarly Technology Group, or STG. STG's tool comes in the form of a web page known as the XML Validation web page, which is accessible online at http://www.stg.brown.edu/service/xmlvalid/. Of course, this validation tool falls into the category of web-based tools. Figure 8.1 shows the STG XML Validation web page.

Figure 8.1. Brown University's Scholarly Technology Group has an XML Validation web page that can be used to validate DTDs.

Similar to most web-based validation tools, there are two approaches available for validating documents with the XML Validation web page:

  • Access the document on the Internet

  • Access the document locally

Depending on your circumstances, the latter option is probably the simplest because you will likely be developing XML documents locally. As I mentioned earlier, sometimes validators have problems with local schemas, so the easier route in terms of having the validator run smoothly is to stick your document(s) and schema on a computer that is accessible on the Internet via a URI. That way you are guaranteeing that the validator can find the document and its schema, both of which are required for validation.

If you're able to post your schema to the Web so that it is available online, you can still use the XML Validation web page to validate local XML documents.

After specifying the document to the XML Validation web page and clicking the Validate button, any errors found during validation will be displayed in your web browser (see Figure 8.2).

Figure 8.2. The STG XML Validation web page reveals errors in an XML document during validation.

The figure reveals errors that were introduced when I deliberately removed the final closing </comments> tag, which invalidates the document. Fortunately, the XML Validation web page caught the problem and alerted me. After repairing the problem and initiating the validation process again, everything turns out fine (see Figure 8.3).

Figure 8.3. The STG XML Validation web page reports that a document is indeed valid.

The online STG Validation web page only validates documents against DTDs, so you won't be able to use it to validate against XSDs.

If you want a quick (and affordable) way to validate local documents against a DTD, then I highly recommend the <oXygen/> XML Editor by SyncRO Soft, which you first learned about in Creating XML Documents. You can download the <oXygen/> XML Editor for free at http://www.oxygenxml.com/. You'll have to register the product in order to run it but registration is entirely free and you can choose not to receive email solicitations during registration. Figure 8.4 shows the <oXygen/> XML Editor reporting an error in a document during validation. The error in this case is the same missing closing </comment> tag error that was shown in Figure 8.2.

Figure 8.4. The <oXygen/> XML Editor is a good tool for performing DTD document validation on local XML documents.

XMLSpy Home Edition is another good free XML editor that supports the validation of XML documents using both DTDs and XSD schemas. To find out more, visit http://www.altova.com/support_freexmlspyhome.asp.

You can tell in the figure that <oXygen/> is extremely informative when it comes to detecting errors and alerting you to them. In fact, in this example the line of code containing the missing </comments> tag is highlighted to indicate where the problem lies. This kind of detailed error analysis is what makes tools such as <oXygen/> worth considering. The <oXygen/> XML Editor is also capable of validating documents against XSDs, which you learn how to do next.

XSD Validation

You'll be glad to know that validating XML documents against an XSD is not much different than validating them against DTDs. It's still important to have the XSD properly associated with the documents, as you learned how to do in the previous lesson. Once that's done, it's basically a matter of feeding the document to a validation tool that is capable of handling XSD schemas. The W3C offers an online XSD validation tool called the W3C Validator for XML Schema, which is located at http://www.w3.org/2001/03/webdata/xsv/. This web-based validator works similarly to the web-based DTD validator you learned about in the previous section. You specify the URI of a document and the validator does all the work. Although the W3C Validator for XML Schema can certainly get the job done, it has a limitation in that you must have the schema file hosted online in order to validate documents.

The underlying XML processor used in the W3C Validator for XML Schema web page is called XSV and is also available from the W3C as a standalone validator. You can download this standalone command-line validator for free from the W3C at http://www.ltg.ed.ac.uk/~ht/xsv-status.html.

An even better online validation tool for use with local files is the online XML Schema Validator, which is very easy to use. This validator is accessible online at http://apps.gotdotnet.com/xmltools/xsdvalidator/. What makes this online validator particularly useful is that it allows you to specify both the XML document and its XSD schema as local files, which means the schema file doesn't have to be publicly accessible online. Figure 8.5 shows the XML Schema Validator successfully validating the training log example document against the etml.xsd schema.

Figure 8.5. The online XML Schema Validator serves as an excellent tool for validating an XML document against an XSD schema.

If you are planning on working with a lot of documents locally or maybe are looking for additional features in a validation tool, I'd return to the familiar <oXygen/> XML Editor that I mentioned in the previous section. In addition to DTD validation, <oXygen/> also supports XSD validation. Figure 8.6 shows the same training log document being successfully validated using the <oXygen/> XML Editor.

Figure 8.6. The <oXygen/> XML Editor is also useful for validating XML documents against XSDs.

Validate Document

The <oXygen/> XML Editor is an excellent and freely available local tool for use in validating XML documents with either DTD or XSD schemas.