Xsd11SchemaValidator.java for XSD 1.1 Validation

This section provides a tutorial example on calling SchemaFactory.newInstance() with 'http://www.w3.org/XML/XMLSchema/v1.1' for XSD 1.1 validation.

After reading documentations from Apache Xerces Website, I see why my XMLReaderValidator.java program failed to validate XSD 1.1 schema documents. The "org.apache.xerces.parsers.SAXParser" class is using a default configuration that assumes XSD 1.0, not XSD 1.1.

In order to support XSD 1.1, we need to find a way to change the configuration setting to use: "http://www.w3.org/XML/XMLSchema/v1.1" as the schema language. Unfortunately, there seems to be no way to modify this setting in the way how the "org.apache.xerces.parsers.SAXParser" class is used in my XMLReaderValidator.java program:

      String parserClass = "org.apache.xerces.parsers.SAXParser";
      String validationFeature 
         = "http://xml.org/sax/features/validation";
      String schemaFeature 
         = "http://apache.org/xml/features/validation/schema";
      try {
         String x = args[0];
         XMLReader r = XMLReaderFactory.createXMLReader(parserClass);

So I need to go back and modify my other schema validation program to use the "javax.xml.validation.SchemaFactory" for XSD 1.1 validation:

/* Xsd11SchemaValidator.java
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Schema;
import javax.xml.XMLConstants;
import javax.xml.transform.sax.SAXSource;
import org.xml.sax.InputSource;
import javax.xml.validation.Validator;
import java.io.*;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ErrorHandler;
class Xsd11SchemaValidator {
  private static int errorCount = 0;
  public static void main(String[] a) {
    if (a.length<2) {
      System.out.println("java Xsd11SchemaValidator schema_file_name "
        + "xml_file_name");
    } else {
      String schemaName = a[0];
      String xmlName = a[1];
      Schema schema = loadSchema(schemaName);
      validateXml(schema, xmlName);
  public static void validateXml(Schema schema, String xmlName) {
    try {
      // creating a Validator instance
      Validator validator = schema.newValidator();

      // setting my own error handler
      validator.setErrorHandler(new MyErrorHandler());

      // preparing the XML file as a SAX source
      SAXSource source = new SAXSource(
        new InputSource(new java.io.FileInputStream(xmlName)));

      // validating the SAX source against the schema
      if (errorCount>0) {
        System.out.println("Failed with errors: "+errorCount);
      } else {

    } catch (Exception e) {
      // catching all validation exceptions
  public static Schema loadSchema(String name) {
    Schema schema = null;
    try {
//      String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
      String language = "http://www.w3.org/XML/XMLSchema/v1.1";
      SchemaFactory factory = SchemaFactory.newInstance(language);
      schema = factory.newSchema(new File(name));
    } catch (Exception e) {
    return schema;
  private static class MyErrorHandler implements ErrorHandler {
    public void warning(SAXParseException e) throws SAXException {
       System.out.println("Warning: "); 
    public void error(SAXParseException e) throws SAXException {
       System.out.println("Error: "); 
    public void fatalError(SAXParseException e) throws SAXException {
       System.out.println("Fattal error: "); 
    private void printException(SAXParseException e) {
      System.out.println("   Line number: "+e.getLineNumber());
      System.out.println("   Column number: "+e.getColumnNumber());
      System.out.println("   Message: "+e.getMessage());

Notice that how the "SchemaFactory.newInstance" method is used to create a new schema object with language="http://www.w3.org/XML/XMLSchema/v1.1" parameter.

Test result is presented in next tutorials.

Last update: 2013.

Table of Contents

 About This Book

 Introduction to XML Schema

 XML Editor and Schema Processor - XMLPad

 Java API for XML Processing - JAXP

 JAXP - XML Schema (XSD) Validation

 Xerces2 Java Parser - Java API of XML Parsers

Using Xerces2 Java API

 XML Schema (XSD) Validation using XMLReader

 Running XMLReaderValidator with Xerces2 2.11.0 Beta

 Running XMLReaderValidator on XSD 1.1 Schema

 XML Schema (XSD) Validation using SAXParser

 SAXParser for XSD Validation in JDK 1.7

 SAXParser for XSD 1.1 Validation

Xsd11SchemaValidator.java for XSD 1.1 Validation

 Xsd11SchemaValidator.java XSD 1.1 Test Result

 XML Schema Language - Basics

 Introduction of XSD Built-in Datatypes

 "string" and Its Derived Datatypes

 "decimal" and Its Derived Datatypes

 "dateTime" and Its Related Datatypes

 Miscellaneous Built-in Datatypes

 Facets, Constraining Facets and Restriction Datatypes

 "simpleType" - Defining Your Own Simple Datatypes

 Complex Element Declaration

 Identity-Constraints: unique, key and keyref

 Assertion as Custom Validation Rules

 XML Schema Location and Namespace in XML Documents

 Overriding Element Types in XML Documents

 Linking Multiple Schema Documents Together



 PDF Printing Version