Section 9.1. Validating XML Documents


9.1. Validating XML Documents

You 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.

Table 9-1. The xmlvalidate task's attributes

Attribute

Description

Required

Default

classname

Specifies the XML parser you want to use.

No

 

classpathref

Specifies where to find the XML parser class. Note that you can also use a nested classpath element.

No

 

failonerror

Specifies you want this task to fail if it encounters an error.

No

true

file

Specifies the file or files you want to validate. You can use a nested fileset.

No

 

lenient

Check only if the XML document is well-formed if true.

No

 

warn

Specifies the parser should log warn messages.

No

 


This task can contain nested dtd elements, which specify locations for DTD resolution. The attributes of the dtd element appear in Table 9-2.

Table 9-2. The dtd element's attributes

Attribute

Description

Required

location

Specifies the location of the DTD you want to use. Set this to a file, a resource, or a URL.

Yes

publicId

Specifies the public ID of the DTD to resolve.

Yes


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.

Table 9-3. The xmlcatalog element's attributes

Attribute

Description

Required

id

Specifies a unique ID for an XMLCatalog. This ID can be used to reference the XMLCatalog from another XMLCatalog.

No

refid

Specifies the ID of another XMLCatalog whose contents are used in this XMLCatalog.

No


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.

Table 9-4. The attribute element's attributes

Attribute

Description

Required

name

Specifies the name of the feature to set

Yes

value

Specifies the boolean setting of the feature

Yes


9.1.1. Validating with XML Schema

It'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 DTDs

Validating 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



    Ant. The Definitive Guide
    Ant: The Definitive Guide, 2nd Edition
    ISBN: 0596006098
    EAN: 2147483647
    Year: 2003
    Pages: 115
    Authors: Steve Holzner

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net