How to validate XML against XSD 1.1 in Java?
Asked Answered
A

5

19

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 use XML Schema 1.1 as I have seen in this code example from the Xerces FAQ.

This is my code:

import java.io.File;
import java.io.IOException;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;

public class XSDValidator {
    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");
        // SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

        // 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 throws 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

As I see it I have exactly the same imports as the code snippet in the Xerces FAQ. I even tried to add Xerces to my Maven dependencies but that didn't help either.

Cheers :)

Aricaarick answered 27/12, 2013 at 20:1 Comment(4)
Where "http://www.w3.org/XML/XMLSchema/v1.1" came from?Reviviscence
@PM77-1 w3.org/TR/xmlschema11-1/#langidsPaedogenesis
It seems that javax.xml uses W3C_XML_SCHEMA_NS_URI for this purpose.Reviviscence
If I use that it's the 1.0 schema.Aricaarick
S
8

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")

UPDATE 2021-09-04 In recent Saxon releases, XSD 1.1 is the default, and no special property needs to be set to enable it.

Striptease answered 28/12, 2013 at 9:48 Comment(8)
I meant, use SchemaFactory.newInstance() after putting Xerces or Saxon on the classpath, and/or setting the appropriate system property to determine which processor you want to load.Striptease
Its not working for , i have set class both of both jar.Honig
Then raise a new question and say exactly what you did and exactly how it failed. We can't help you on the basis of one ungrammatical sentence.Striptease
Does not work for me. Whats the minimum Java/Saxon version I need ? Document test.xml has not been parsed correctly: Property 'saxon.sf.net/feature/xsd-version' is not recognized. Property 'saxon.sf.net/feature/xsd-version' is not recognized. Stacktrace: .....Huxley
It would be nice to have the full code .. I dont know what is the "normal way" to load Saxon or Xerces and I have to google to figure out how to set the appropiate system propertiesHuxley
@MichaelKay: For Saxon it's better to use the constant from FeatureKeys to set properties: SchemaFactory.setProperty(FeatureKeys.XSD_VERSION, "1.1");Tarsuss
@MichaelKay: Unfortunately I get the following exception when trying to set the property: Caused by: org.xml.sax.SAXNotRecognizedException: Property 'http://saxon.sf.net/feature/xsd-version' is not recognized..Tarsuss
Please raise a new question and explain exactly what you are doing and how it fails.Striptease
J
19

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>
    
Jacinto answered 8/8, 2015 at 2:55 Comment(2)
Thanks alot, finally this seems to be the right .jar There are comments arrounf on SO which say that xercesImpl / 2.11.0 could be used to validate xsd1.1 , but that seems not to be the caseHuxley
That's the way to do it. Also with the release 2.12.0. Additionally I had to set the cta-full-xpath-checking feature in the schemafactory: SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1"); schemaFactory.setFeature("http://apache.org/xml/features/validation/cta-full-xpath-checking",true); return schemaFactory;Tarsuss
S
8

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")

UPDATE 2021-09-04 In recent Saxon releases, XSD 1.1 is the default, and no special property needs to be set to enable it.

Striptease answered 28/12, 2013 at 9:48 Comment(8)
I meant, use SchemaFactory.newInstance() after putting Xerces or Saxon on the classpath, and/or setting the appropriate system property to determine which processor you want to load.Striptease
Its not working for , i have set class both of both jar.Honig
Then raise a new question and say exactly what you did and exactly how it failed. We can't help you on the basis of one ungrammatical sentence.Striptease
Does not work for me. Whats the minimum Java/Saxon version I need ? Document test.xml has not been parsed correctly: Property 'saxon.sf.net/feature/xsd-version' is not recognized. Property 'saxon.sf.net/feature/xsd-version' is not recognized. Stacktrace: .....Huxley
It would be nice to have the full code .. I dont know what is the "normal way" to load Saxon or Xerces and I have to google to figure out how to set the appropiate system propertiesHuxley
@MichaelKay: For Saxon it's better to use the constant from FeatureKeys to set properties: SchemaFactory.setProperty(FeatureKeys.XSD_VERSION, "1.1");Tarsuss
@MichaelKay: Unfortunately I get the following exception when trying to set the property: Caused by: org.xml.sax.SAXNotRecognizedException: Property 'http://saxon.sf.net/feature/xsd-version' is not recognized..Tarsuss
Please raise a new question and explain exactly what you are doing and how it fails.Striptease
C
2

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.

Coarse answered 27/10, 2020 at 5:29 Comment(0)
F
1

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.

Flabellum answered 25/4, 2018 at 13:43 Comment(2)
Both links are broken and lead to a 404 not found site.Tarsuss
@thehandofNOD, apologies for the broken links. I have updated my post. The artifacts for this validator will be released to Maven Central next week (week of 2019-05-06).Flabellum
H
0

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

Hover answered 23/6, 2022 at 12:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.