Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate XML against XSD 1.1 in Java?

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

like image 571
Vogon Jeltz Avatar asked Dec 27 '13 20:12

Vogon Jeltz


People also ask

How do you validate against XSD?

Simply go to the XML Tools > Validate Now option and click on it. You can also press Ctrl + Alt + Shift + M key combination to open Validate Now option. Now, select the XSD file against which you want to validate the opened XML document. Simply browse and then import the XSD file in the respective field.

What is XML validation against XSD?

XML Validator - XSD (XML Schema) XSD files are "XML Schemas" that describe the structure of a XML document. The validator checks for well formedness first, meaning that your XML file must be parsable using a DOM/SAX parser, and only then does it validate your XML against the XML Schema.

Can we validate XML documents against a schema?

XML documents can be validated against an XML schema definition language (XSD) schema in an XmlSchemaSet.


2 Answers

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>
    
like image 50
Mustafa Avatar answered Nov 03 '22 11:11

Mustafa


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.

like image 34
Michael Kay Avatar answered Nov 03 '22 09:11

Michael Kay