SAXParser for XSD Validation Fixed

This section describes a tutorial example on how to the Xerces2 SAXParser class included in JDK to perform XSD validation on an XML document. The XSD document must be specified with SAXParser.setProperty() method.

To fix the XSD loading problem in SAXValidator.java, I revised the program using the SAXParser.setProperty() method to set the XSD document explicitly. Here is the revised program, SAXValidatorXsd.java:

/* SAXValidatorXsd.java
 - Copyright (c) 2002-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 SAXValidatorXsd {
   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/2001/XMLSchema");

         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 JDK 1.7 to JDK 13 without Xerces2 JAR files, I get the same result:

herong> java SAXValidatorXsd.java dictionary_invalid_xsd.xml dictionary.xsd
com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl@4387b79e
com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl@1ffe63b9

Error:
   Public ID: null
   System ID: file:/C:/herong/dictionary_invalid_xsd.xml
   Line number: 6
   Column number: 49
   Message: cvc-complex-type.3.2.2: Attribute 'xsi:noNamespaceSchemaLocation'
      is not allowed to appear in element 'dictionary'.

Error:
   Public ID: null
   System ID: file:/C:/herong/dictionary_invalid_xsd.xml
   Line number: 7
   Column number: 22
   Message: cvc-datatype-valid.1.2.1: 'yes' is not a valid value for
      'boolean'.

Error:
   Public ID: null
   System ID: file:/C:/herong/dictionary_invalid_xsd.xml
   Line number: 7
   Column number: 22
   Message: cvc-attribute.3: The value 'yes' of attribute 'acronym' on
      element 'word' is not valid with respect to its type, 'boolean'.
...

Running it with JDK 13 and with Xerces2 2.12.1 JAR files, I get the same result:

herong> java_xerces SAXValidatorXsd.java dictionary_invalid_xsd.xml dictionary.xsd
org.apache.xerces.jaxp.SAXParserFactoryImpl@d4342c2
org.apache.xerces.jaxp.SAXParserImpl@55182842
Error:
   Public ID: null
   System ID: file:/Users/herong/write/xsd_20080000/cod/xerces2/dictionary_invalid_xsd.xml
   Line number: 6
   Column number: 49
   Message: cvc-complex-type.3.2.2: Attribute 'xsi:noNamespaceSchemaLocation'
      is not allowed to appear in element 'dictionary'.
...

Interestingly, both implementations in JDK and in Xerces2 JAR files do not like the attribute xsi:noNamespaceSchemaLocation="..." used in the XML document to specify the XSD document. I have no idea why.

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 APIs

 JAXP, DOM and SAX APIs in Xerces2 JARs

 XML Schema (XSD) Validation using XMLReader

 Running XMLReaderValidator on XSD 1.1 Schema

 XML Schema (XSD) Validation using SAXParser

SAXParser for XSD Validation Fixed

 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

 Archived Tutorials

 References

 Full Version in PDF/EPUB