XML validation against XSD 1.1 with Xerces in Java
Asked Answered
E

4

10

I have installed Xerces through Maven:

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jdom</groupId>
        <artifactId>jdom</artifactId>
        <version>2.0.2</version>
    </dependency>
    <dependency>
        <groupId>xerces</groupId>
        <artifactId>xercesImpl</artifactId>
        <version>2.11.0</version>
    </dependency>            
</dependencies>

I then tried the code given in this example from the Xerces FAQ to validate a XML file against a schema in version 1.1. This is my code:

private static void validateFile(File xmlFile, File xsdFile) throws SAXException, IOException
{
    // 1. Lookup a factory for the W3C XML Schema language
    SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1");

    // 2. Compile the schema.
    File schemaLocation = xsdFile;
    Schema schema = factory.newSchema(schemaLocation);

    // 3. Get a validator from the schema.
    Validator validator = schema.newValidator();

    // 4. Parse the document you want to check.
    Source source = new StreamSource(xmlFile);

    // 5. Check the document
    try
    {
        validator.validate(source);
        System.out.println(xmlFile.getName() + " is valid.");
    }
    catch (SAXException ex)
    {
        System.out.println(xmlFile.getName() + " is not valid because ");
        System.out.println(ex.getMessage());
    }
}

The code only yields this exception:

java.lang.IllegalArgumentException: No SchemaFactory that implements the schema language specified by: http://www.w3.org/XML/XMLSchema/v1.1 could be loaded
at javax.xml.validation.SchemaFactory.newInstance(SchemaFactory.java:204)
at example.xml.XSDValidator.validateFile(XSDValidator.java:65)

Seems like I failed to configure/install Xerces correctly. Please help me get this working, the XML files force me to use the schema in 1.1, I got a normal validator for 1.0 running but I have huge problems with this. I appreciate every hint!

Ecotone answered 28/12, 2013 at 20:32 Comment(2)
#2397403Orpah
That didn't help. The problem I am having is that I want to validate against XSD 1.1 and not 1.0Ecotone
O
7

It looks that you need Xerces2 Java 2.11.0 (XML Schema 1.1) (Beta) version, which isn't in maven repository. You can download it from Xerces website, and install it to your local maven repository: mvn install:install-file -Dfile=xercesImpl.jar -DgroupId=xerces -DartifactId=xercesImpl -Dversion=2.11.0.beta -Dpackaging=jar Then you will be able to include it in your Maven project dependencies:

<dependency>
    <groupId>xerces</groupId>
    <artifactId>xercesImpl</artifactId>
    <version>2.11.0.beta</version>
</dependency>   
Orpah answered 28/12, 2013 at 20:43 Comment(3)
Thanks for the quick reply! I downloaded the Xerces2 Java 2.11.0 (XML Schema 1.1) (Beta) - zip binary distribution and then tried to install it. When I use your mvn command the only answer I get is what you get when you type "mvn -version". There is no error or confirmation message. And adding the dependency leads to an error, so I suspect the install failed. Do I have to put the xercesImpl.jar to a specific folder before I can run that command? I also tried giving it the full path to the file.Ecotone
Sorry! It's my fault. -version=2.11.0.beta should be -Dversion=2.11.0.beta. So full command is now: mvn install:install-file -Dfile=xercesImpl.jar -DgroupId=xerces -DartifactId=xercesImpl -Dversion=2.11.0.beta -Dpackaging=jar I checked it for you and after installing it to local repository, and injecting into pom.xml with version 2.11.0.beta, no exception is thrown from schema factory. Try it out, good luckOrpah
Oh boy, I could have seen that! :D Thank you so much, I got it working now. I had to add the org.eclipse.wst.xml.xpath2.processor_1.1.0.jar also because otherwise I got a classNotFound exception. That jar is found in the same folder as the xercesImpl.jar. Thanks again!Ecotone
V
6

I will add another answer, because for me this dependency did not work (same error as described by OP):

<dependency>
  <groupId>xerces</groupId>
  <artifactId>xercesImpl</artifactId>
  <version>2.11.0</version>
</dependency>

I quess 2.11.0 should be newer than 2.11.0.beta, but it seems like xsd1.1 is not supported in that version !

Instead only the following dependency lead to a working XSD1.1 validation for me:

<dependency>
    <groupId>org.opengis.cite.xerces</groupId>
    <artifactId>xercesImpl-xsd11</artifactId>
    <version>2.12-beta-r1667115</version>
</dependency>

( Found in this SO thread: How to validate XML against XSD 1.1 in Java? )

Variegate answered 14/3, 2016 at 15:12 Comment(2)
Thank you, that works for me as well. Do you have any information who develops this version 2.12 or if it is official? I could not find any information about 2.12 on the official Apache Xerces website.Ecotone
Me neither, I just struggled over the linked Stack Overflow thread, from where I got the working dependency. Maybe the author of the post knows more.Variegate
G
1

I think they have added version 2.11 to maven now. The following dependency in Maven works out-of-the-box:

<dependency>
  <groupId>xerces</groupId>
  <artifactId>xercesImpl</artifactId>
  <version>2.11.0</version>
</dependency>
Gaughan answered 10/1, 2016 at 15:56 Comment(1)
"Out of the box" is different. Maybe you know whats wrong for me: #35942113Variegate
U
1

Xerces-J provides a "fully compliant XML Schema 1.1 implementation" since version 2.12.0, see release history here: https://xerces.apache.org/news.html.

Xerces2 Java 2.12.2 (XML Schema 1.1) - tar.gz [PGP] [SHA] Just got released earlier this week. https://xerces.apache.org/mirrors.cgi#notes

Uzial answered 28/1, 2022 at 19:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.