ANT XML validation with xerces

Having trouble installing Oxygen? Got a bug to report? Post it all here.
jvanhille
Posts: 8
Joined: Tue Jan 27, 2015 2:36 pm

ANT XML validation with xerces

Post 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
Radu
Posts: 9421
Joined: Fri Jul 09, 2004 5:18 pm

Re: ANT XML validation with xerces

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
jvanhille
Posts: 8
Joined: Tue Jan 27, 2015 2:36 pm

Re: ANT XML validation with xerces

Post 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
Radu
Posts: 9421
Joined: Fri Jul 09, 2004 5:18 pm

Re: ANT XML validation with xerces

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply