XSD Schema XML SAX Validation Errors

This section provides some errors from the XSD schema validation process on invalid XML files represented as SAXSource objects using the JAXP API.

In the previous section, we tested a valid XML file that conforms to the XSD schema file. In this section, we will look at two invalid XML files and see how XsdSchemaSaxValidator.java generates error messages.

The first invalid XML file has an extra node (element) inside "html":

>type first_html_extra_node.xml
<?xml version="1.0" encoding="utf-8"?>
<html>
<body>
<p>My first HTML document in XML format.</p>
</body>
</html>

>java XsdSchemaSaxValidator first_html.xsd first_html_extra_node.xml

Validator Class: com.sun.org.apache.xerces.internal.jaxp
   .validation.ValidatorImpl

org.xml.sax.SAXParseException: cvc-type.3.1.2: 
Element 'body' is a simple type,
so it must have no element information item [children].

The output shows that the "body" element has an extra children element, which is not allowed by the schema.

The second invalid XML file has an extra attribute and an extra node (element) inside "html":

>type first_html_extra_attribute.xml
<?xml version="1.0" encoding="utf-8"?>
<html>
<body bgcolor="#eeeeee">
<p>My first HTML document in XML format.</p>
</body>
</html>

>java XsdSchemaSaxValidator first_html.xsd
   first_html_extra_attribute.xml

Validator Class: com.sun.org.apache.xerces.internal.jaxp
   .validation.ValidatorImpl

org.xml.sax.SAXParseException: cvc-type.3.1.1: 
Element 'body' is a simple type,
so it cannot have attributes, excepting those whose namespace name
is identical to 'http://www.w3.org/2001/XMLSchema-instance'
and whose [local name] is one of 'type', 'nil', 'schemaLocation'
or 'noNamespaceSchemaLocation'. However, the attribute, 'bgcolor' 
was found.

The error message in the output is very clear. The attribute "bgcolor" is not allowed in the element "body".

However, the validator failed to report the second error that the "p" element is not allowed inside "body".

If you compare XsdSchemaSaxValidator.java with XsdSchemaDomValidator.java, you will see that the Validator instance handles validation errors in the same way for both DOMSource objects and SAXSource objects.

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

 Standard Steps to Validate XML Documents Against a Schema

 XSD Schema File Loader - XsdSchemaLoader.java

 XSD Schema File Loading Errors

 XSD Schema XML DOM Validator - XsdSchemaDomValidator.java

 XSD Schema XML DOM Validation Errors

 XSD Schema XML DOM Validator with Error Handler

 XSD Schema XML SAX Validator - XsdSchemaSaxValidator.java

XSD Schema XML SAX Validation Errors

 XSD Schema XML SAX Validator with Error Handler

 XSD Schema XML Validator - Final Version

 Xerces2 Java Parser - Java API of XML Parsers

 Using Xerces2 Java API

 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