tags:

views:

372

answers:

2

I am attempting to use Ant's XMLValidate task to validate an XML document against a DTD. The problem is not that it doesn't work, but that it works too well. My DTD contains an xref element with an "@linkend" attribute of type IDREF. Most of these reference IDs outside of the current document. Because of this, my build fails, since the parser complains that the ID that the IDREF is referencing doesn't exist. So, is there any way that I can validate my XML document against the DTD, but ignore errors of this type?

A few things I've tried: Setting the "lenient" option on XMLValidate makes the task only check the document's well-formedness, not it's validity against a DTD. The XMLValidate task in the Ant manual lists some JAXP and SAX options you can set, but none seem applicable.

Here's my code:

 <target name="validate">
        <echo message="Validating ${input}"/>
        <xmlvalidate file="${input}" failonerror="yes"
            classname="org.apache.xml.resolver.tools.ResolvingXMLReader">
            <classpath refid="xslt.processor.classpath"/>
        </xmlvalidate>
 </target>

As you can see, I'm using ResolvingXMLReader to resolve the DTD against a catalog of public identifiers. However, I get the same behavior if I specify the DTD directly using a nested xmlcatalog element.

A: 

Not sure if this helps, but could you try this workaround? Create a temporary file, merge all your XMLs, and do the validation.

mindas
+1  A: 

Your problem derives from the difference between two interpretations of the DTD: yours, and the spec's :-). IDREFs must refer to ids in the same document, whereas yours refer to elements across documents.

My suggestion is to create your own version of the DTD that specifies NMTOKEN instead of IDREF for that attribute, and use it to perform your validation. This will ensure that the contents will be valid xml id values.

Warren Blanchet
Good point. This is actually what I ended up doing.
James Sulak