XSD Tutorials - Herong's Tutorial Examples - Version 5.10, by Dr. Herong Yang

SAXParser for XSD 1.1 Validation

This section describes a tutorial example trying to use the Xerces2 SAXParser class included in JDK 1.7 to perform XSD 1.1 validation on an XML document. The program fails.

Now let's try to see if we make SAXValidatorJdk17.java program with XSD 1.1 by setting "http://www.w3.org/XML/XMLSchema/v1.1" as the schema language property:

/* SAXValidatorJdk17Xsd11.java
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
 */
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
class SAXValidatorJdk17Xsd11 {
   public static void main(String[] args) {
      String schemaFeature 
         = "http://apache.org/xml/features/validation/schema";
      try {
         File x = new File(args[0]);
         String schemaLocation = args[1];
         
         SAXParserFactory f = SAXParserFactory.newInstance();
         System.out.println(f.toString());
         f.setValidating(true);
         f.setFeature(schemaFeature,true);

         SAXParser p = f.newSAXParser();
         System.out.println(p.toString());

         p.setProperty(
            "http://apache.org/xml/properties/schema"
            +"/external-noNamespaceSchemaLocation",schemaLocation);

         p.setProperty(
            "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            "http://www.w3.org/XML/XMLSchema/v1.1");
         
         DefaultHandler h = new MyErrorHandler();
         p.parse(x,h);
      } catch (ParserConfigurationException e) {
         System.out.println(e.toString()); 
      } catch (SAXException e) {
         System.out.println(e.toString()); 
      } catch (IOException e) {
         System.out.println(e.toString()); 
      }
   }
   private static class MyErrorHandler extends DefaultHandler {
      public void warning(SAXParseException e) throws SAXException {
         System.out.println("Warning: "); 
         printInfo(e);
      }
      public void error(SAXParseException e) throws SAXException {
         System.out.println("Error: "); 
         printInfo(e);
      }
      public void fatalError(SAXParseException e) throws SAXException {
         System.out.println("Fattal error: "); 
         printInfo(e);
      }
      private void printInfo(SAXParseException e) {
         System.out.println("   Public ID: "+e.getPublicId());
         System.out.println("   System ID: "+e.getSystemId());
         System.out.println("   Line number: "+e.getLineNumber());
         System.out.println("   Column number: "+e.getColumnNumber());
         System.out.println("   Message: "+e.getMessage());
      }
   }
}

Running it with the "Xerces2 2.11.0 (XML Schema 1.1) (Beta)" version:

c:\Progra~1\Java\jdk1.7.0_07\bin\javac SAXValidatorJdk17Xsd11.java

c:\Progra~1\Java\jdk1.7.0_07\bin\java 
   -cp ".;c:\local\xerces-2_11_0-xml-schema-1.1-beta\xercesImpl.jar;
   c:\local\xerces-2_11_0-xml-schema-1.1-beta
   \org.eclipse.wst.xml.xpath2.processor_1.1.0.jar"
   SAXValidatorJdk17Xsd11 dictionary_invalid_xsd.xml dictionary.xsd

org.apache.xerces.jaxp.SAXParserFactoryImpl@b1c260
org.apache.xerces.jaxp.SAXParserImpl@12c4c57
org.xml.sax.SAXNotSupportedException: The specified schema language 
   is not supported.

Too bad, the SAXParse object created with the default configuration does not recognize the XSD 1.1 URI: http://www.w3.org/XML/XMLSchema/v1.1.

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

 Glossary

 References

 PDF Printing Version

SAXParser for XSD 1.1 Validation - Updated in 2014, by Dr. Herong Yang