How to validate XML against XSD 1.1 in Java?

后端 未结 4 558
花落未央
花落未央 2020-12-01 14:55

What is the best way to validate XML files against XML Schema 1.1 in Java?

I took the code from this tutorial and changed the line where it looks up the factory to u

相关标签:
4条回答
  • 2020-12-01 15:08

    There is a generic XML validator that works with XML Schema v1.1, which uses xercesImpl-xsd11 2.12-beta-r1667115. The validator is available here as a Maven plugin, and here as an embeddable library.

    0 讨论(0)
  • 2020-12-01 15:12

    Unfortunately, neither the JDK bundled version (as of Java 8) nor the latest official version from maven central (2.11.0) contains XSD 1.1 implementation.

    You actually need the 2.11.0-xml-schema-1.1-beta version of Xerces to able to run the example in the FAQ you have linked.

    You may do one of the following.

    1. Download the Xerces2 Java 2.11.0 (XML Schema 1.1) (Beta) binaries from Xerces website and manually add jars to the classpath (or install locally via Maven). Link: http://xerces.apache.org/mirrors.cgi. You need at least the following:

      cupv10k-runtime.jar
      org.eclipse.wst.xml.xpath2.processor_1.1.0.jar
      xercesImpl.jar
      xml-apis.jar
      
    2. Use the following unofficial maven dependency.

      <dependency>
          <groupId>org.opengis.cite.xerces</groupId>
          <artifactId>xercesImpl-xsd11</artifactId>
          <version>2.12-beta-r1667115</version>
      </dependency>
      
    0 讨论(0)
  • 2020-12-01 15:15

    I don't think you can use the JAXP service mechanism to search for an XSD 1.1 processor. Load Saxon or Xerces in the normal way, and then enable XSD 1.1 processing. For Saxon this is done using

    SchemaFactory.setProperty("http://saxon.sf.net/feature/xsd-version", "1.1")
    
    0 讨论(0)
  • 2020-12-01 15:15

    After searching for a while, this tutorial contains these dependency worked for me:

    <dependency>
        <groupId>org.exist-db.thirdparty.xerces</groupId>
        <artifactId>xercesImpl</artifactId>
        <version>2.12.0</version>
        <classifier>xml-schema-1.1</classifier>
    </dependency>
    
    <!-- xpath2 and java-cup are needed at runtime
            for xercesImpl Schema 1.1 support -->
    <dependency>
        <groupId>org.exist-db.thirdparty.org.eclipse.wst.xml</groupId>
        <artifactId>xpath2</artifactId>
        <version>1.2.0</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>edu.princeton.cup</groupId>
        <artifactId>java-cup</artifactId>
        <version>10k</version>
        <scope>runtime</scope>
    </dependency>
    

    Unfortunately, these libraries don't seem to be official. So, use it at your own risk.

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