Validate an XML File Against Multiple Schema Definitions

前端 未结 8 1536
囚心锁ツ
囚心锁ツ 2020-12-01 09:23

I\'m trying to validate an XML file against a number of different schemas (apologies for the contrived example):

  • a.xsd
  • b.xsd
  • c.xsd
相关标签:
8条回答
  • 2020-12-01 09:54

    So just in case anybody else runs into the same issue here, I needed to load a parent schema (and implicit child schemas) from a unit test - as a resource - to validate an XML String. I used the Xerces XMLSchemFactory to do this along with the Java 6 validator.

    In order to load the child schema's correctly via an include I had to write a custom resource resolver. Code can be found here:

    https://code.google.com/p/xmlsanity/source/browse/src/com/arc90/xmlsanity/validation/ResourceResolver.java

    To use the resolver specify it on the schema factory:

    xmlSchemaFactory.setResourceResolver(new ResourceResolver());
    

    and it will use it to resolve your resources via the classpath (in my case from src/main/resources). Any comments are welcome on this...

    0 讨论(0)
  • 2020-12-01 09:54

    The schema stuff in Xerces is (a) very, very pedantic, and (b) gives utterly useless error messages when it doesn't like what it finds. It's a frustrating combination.

    The schema stuff in python may be a lot more forgiving, and was letting small errors in the schema go past unreported.

    Now if, as you say, c.xsd includes b.xsd, and b.xsd includes a.xsd, then there's no need to load all three into the schema factory. Not only is it unnecessary, it will likely confuse Xerces and result in errors, so this may be your problem. Just pass c.xsd to the factory, and let it resolve b.xsd and a.xsd itself, which it should do relative to c.xsd.

    0 讨论(0)
提交回复
热议问题