|
9.1. Validating XML DocumentsYou use the xmlvalidate task to validate XML documents with Document Type Definitions (DTDs) or XML schema. Your build process may involve generating XML documents, and it can be worthwhile to test those documents for validity before deploying them (for example, see the section on XDoclet in this chapter. You typically use XDoclet to generate XML documents for deploying web applications, such as web.xml, application.xml, and so on). By default, this task uses the SAX2 parser implementation provided by Sun's JAXP (http://java.sun.com/xml/jaxp/index.jsp). The attributes of this task appear in Table 9-1.
This task can contain nested dtd elements, which specify locations for DTD resolution. The attributes of the dtd element appear in Table 9-2.
You can use nested xmlcatalog elements; an XMLCatalog is a catalog of public resources, such as DTDs or entities, that are referenced in an XML document. The attributes of this element appear in Table 9-3.
The xmlcatalog element can contain nested dtd, classpath, and catalogpath elements; the catalogpath element is an Ant path-like element, which, as you know, means it can contain nested fileset elements, pathelement elements, and so on. The xmlvalidate element can contain nested attribute elements, which set SAX Parser features as defined at http://xml.org/sax/features/. The attributes of this element appear in Table 9-4.
9.1.1. Validating with XML SchemaIt's easy enough to use the xmlvalidate task to validate XML documents with schema, as shown in Example 9-1, which validates document.xml. This example turns on schema validation by setting the parser attribute identified by the URI http://apache.org/xml/features/validation/schema to true. Example 9-1. Valiating with a schema (ch09/schemat/build.xml)<?xml version="1.0" encoding="UTF-8" ?> <project default="main" > <target name="main"> <xmlvalidate lenient="no" warn="yes" file="document.xml" classname="org.apache.xerces.parsers.SAXParser"> <attribute name="http://apache.org/xml/features/validation/schema" value="true" /> </xmlvalidate> </target> </project> Here's document.xml, the document to validate, which uses the xsi:noNamespace SchemaLocation attribute to specify the name of the schema document, document.xsd: <?xml version="1.0" encoding="UTF-8" ?> <document xsi:noNamespaceSchemaLocation="document.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > <hello /> </document> Here's the schema, document.xsd: <?xml version="1.0" encoding="UTF-8" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" elementFormDefault="qualified" > <xs:element name="document"> <xs:complexType mixed="false"> <xs:sequence> <xs:element name="hello" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> Finally, here's what you see when you execute this build file, showing that the document was successfully validated: %ant Buildfile: build.xml main: [xmlvalidate] 1 file(s) have been successfully validated. BUILD SUCCESSFUL Total time: 1 second Here's what you'd see if there was an error; for example, if "document" had been mispelled "documnt" in the schema: %ant Buildfile: build.xml main: [xmlvalidate] document.xml:4:60: cvc-elt.1 Cannot find the declaration of 'document'. BUILD FAILED document.xml is not a valid XML document. Total time: 1 second 9.1.2. Validating with DTDsValidating documents using DTDs is as easy, as you can see in Example 9-2, which validates a document, document.xml, with a DTD. Example 9-2. Valiating with a DTD ch09/DTD/build.xml<?xml version="1.0" encoding="UTF-8" ?> <project default="main" > <target name="main"> <xmlvalidate lenient="no" warn="yes" file="document.xml" classname="org.apache.xerces.parsers.SAXParser"> </xmlvalidate> </target> </project> Here's the document with a DTD, document.xml, to validate: <?xml version = "1.0" standalone="yes"?> <!DOCTYPE document [ <!ELEMENT document (employee)*> <!ELEMENT employee (name, hiredate, projects)> <!ELEMENT name (lastname, firstname)> <!ELEMENT lastname (#PCDATA)> <!ELEMENT firstname (#PCDATA)> <!ELEMENT hiredate (#PCDATA)> <!ELEMENT projects (project)*> <!ELEMENT project (product,id,price)> <!ELEMENT product (#PCDATA)> <!ELEMENT id (#PCDATA)> <!ELEMENT price (#PCDATA)> ]> <document> <employee> <name> <lastname>Kelly</lastname> <firstname>Grace</firstname> </name> <hiredate>October 15, 2005</hiredate> <projects> <project> <product>Printer</product> <id>111</id> <price>$111.00</price> </project> <project> <product>Laptop</product> <id>222</id> <price>$989.00</price> </project> </projects> </employee> <employee> <name> <lastname>Grant</lastname> <firstname>Cary</firstname> </name> <hiredate>October 20, 2005</hiredate> <projects> <project> <product>Desktop</product> <id>333</id> <price>$2995.00</price> </project> <project> <product>Scanner</product> <id>444</id> <price>$200.00</price> </project> </projects> </employee> <employee> <name> <lastname>Gable</lastname> <firstname>Clark</firstname> </name> <hiredate>October 25, 2005</hiredate> <projects> <project> <product>Keyboard</product> <id>555</id> <price>$129.00</price> </project> <project> <product>Mouse</product> <id>666</id> <price>$25.00</price> </project> </projects> </employee> </document> And here are the results: %ant Buildfile: build.xml main: [xmlvalidate] 1 file(s) have been successfully validated. BUILD SUCCESSFUL Total time: 0 seconds |
|