Page 1 of 1

ANT XML validation with xerces

Posted: Fri Nov 27, 2015 5:03 pm
by jvanhille
Hello,

I tried to validate some xml files with xerces parser launch with ANT script. My XML files have a DOCTYPE declaration to define the ENTITY graphic not to define a dtd. My XML files must be validate against a schema. See below an example :

Code: Select all


<!DOCTYPE task [
<!ENTITY ICN-LEAP-1A-725800-B-F0301-00285-A-001-01 SYSTEM "ICN-LEAP-1A-725800-B-F0301-00285-A-001-01.cgm" NDATA cgm>
<!NOTATION cgm SYSTEM "cgm">]><task xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" chapnbr="72" sectnbr="24".....
xsi:noNamespaceSchemaLocation="http://www.snecma.com/techpub/schemas/ata/em.xsd">
<effect>......
When I execute my script I obtained :

Code: Select all


[schemavalidate] ....procedure.xml:18:88: Element type "task" must be declared.
[schemavalidate] ....procedure.xml:19:12: Element type "effect" must be declared.
[schemavalidate] ....procedure.xml:23:33: Element type "sbeff" must be declared.
[schemavalidate] ....procedure.xml:37:55: cvc-complex-type.2.4.d: Invalid content was found starting with element 'effect'. No child element is expected at this point
....
...
My ANT script is :

Code: Select all

    <schemavalidate failonerror="true" lenient="no" warn="yes"
classname="org.apache.xerces.parsers.SAXParser">
<fileset dir="${outdir}" includes="*.xml"/>
<classpath location="../tools/xercesImpl.jar"/>

<attribute name="http://apache.org/xml/features/validation/schema" value="true"/>
<attribute name="http://xml.org/sax/features/validation" value="true"/>
<xmlcatalog>
<catalogpath location="..\catalog.xml"/>
</xmlcatalog>
</schemavalidate>
The parser try to found a DTD in the DOCTYPE declaration. The validation with my xsd works (the last message come from the validation against my schema).

How can I tell to xerces to ignore the doctype?

Obsiously before to ask here, I searched on google but I didn't find the feature to use.

Thank for your help.

Regards

Re: ANT XML validation with xerces

Posted: Mon Nov 30, 2015 10:54 am
by Radu
Hi,

I'm not sure if this can be made to work or not, never tried it myself.

Looking at the ANT documentation for the task you are using:

https://ant.apache.org/manual/Tasks/schemavalidate.html

it seems to have a disableDTD attribute but I'm not sure if it would help you or not.

Regards,
Radu

Re: ANT XML validation with xerces

Posted: Mon Nov 30, 2015 6:50 pm
by jvanhille
Hello Radu,

It seems that it is not possible to use xerces parser to do that (see https://xerces.apache.org/xerces2-j/faq-pcfp.html#faq-4). It is strange because when I validate my xml file in Oxygen by using xerces I don't have the problem.

I found a solution who is to use the default parser implemented by ant by default (SAX2 parser implementation provided by JAXP).

I wrote this and it works:

Code: Select all

    <xmlvalidate failonerror="false" lenient="no" warn="yes">
<fileset dir="${outdir}" includes="*.xml"/>
<!--<classpath location="../tools/xercesImpl.jar"/> -->

<!--classname="org.apache.xerces.parsers.SAXParser"-->
<attribute name="http://apache.org/xml/features/validation/schema" value="true"/>
<attribute name="http://xml.org/sax/features/validation" value="true"/>
<attribute name="http://xml.org/sax/features/namespaces" value="true"/>
<xmlcatalog>
<catalogpath location="..\catalog.xml"/>
</xmlcatalog>
<property name="http://java.sun.com/xml/jaxp/properties/schemaLanguage" value="http://www.w3.org/2001/XMLSchema"/>
</xmlvalidate>
I put the property http://java.sun.com/xml/jaxp/properties/schemaLanguage for do not report DTD validation errors.

Best regards

Re: ANT XML validation with xerces

Posted: Tue Dec 01, 2015 9:10 am
by Radu
Hi,

About this remark:
It is strange because when I validate my xml file in Oxygen by using xerces I don't have the problem.
Oxygen also uses Xerces for validation but it completely controls the Xerces parser by directly using its Java API.
For example in cases in which the XML has a DTD associated we first do a detection to see if there are any element definitions in the DTD. If not, we realize the DTD is used only to store entity definitions and we force Xerces only to validate using the other detected schemas.

Regards,
Radu