XML Tutorials - Herong's Tutorial Examples - v5.25, by Herong Yang
SAXParseException - 'xsi:noNamespaceSchemaLocation' Not Allowed
This section provides a tutorial example showing the SAXParseException that says Attribute 'xsi:noNamespaceSchemaLocation' is not allowed when applying XSD schema validation on an XML DOM object.
In previous tutorials, we tested XML validation using a javax.xml.validation.Validator object created from a loaded XSD schema file. The Validator object is then applied to a XML DOM object or a SAXSource object that represents an XML file.
In those tests, the XML file has no reference to XSD schema file. Now, let's do some tests on an XML file that contains a reference to the XSD file using the 'xsi:noNamespaceSchemaLocation' attribute. Here is an example XML file, dictionary_invalid_xsd.xml:
<?xml version="1.0"?> <dictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="dictionary.xsd"> <!-- dictionary_invalid_xsd.xml - Copyright (c) 2002-2018 HerongYang.com. All Rights Reserved. --> <word acronym="yes"> <name>XML</name> <definition reference="Herong's Notes">eXtensible Markup Language.</definition> <update date="23-Dec-2014"/> </word> <word symbol="true"> <name><</name> <definition>Mathematical symbol representing the "less than" logical operation, like: 1<2.</definition> <definition>Reserved symbol in XML representing the beginning of tags, like: <![CDATA[<p>Hello world!</p>]]> </definition> <update editor="Herong Yang"/> </word> <word symbol="no" acronym="false"> <name>extensible</name> <definition>Capable of being extended.</definition> </word> </dictionary>
Here is the XSD file, dictionary.xsd:
<?xml version="1.0"?> <!-- dictionary.xsd - Copyright (c) 2002-2018 HerongYang.com. All Rights Reserved. --> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="dictionary" type="dictionaryType"/> <xsd:complexType name="dictionaryType"> <xsd:sequence> <xsd:element name="word" type="wordType" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="wordType"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="definition" type="definitionType" maxOccurs="unbounded"/> <xsd:element name="update" type="updateType" minOccurs="0"/> </xsd:sequence> <xsd:attribute name="acronym" type="xsd:boolean" use="optional"/> <xsd:attribute name="symbol" type="xsd:boolean" use="optional"/> </xsd:complexType> <xsd:complexType name="definitionType" mixed="true"> <xsd:attribute name="reference" type="xsd:string"/> </xsd:complexType> <xsd:complexType name="updateType"> <xsd:attribute name="date"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:pattern value="\p{Nd}{4}-\p{Nd}{2}-\p{Nd}{2}"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> </xsd:complexType> </xsd:schema>
Run XsdSchemaSaxValidatorWithErrorHandler.java with JDK:
herong> java XsdSchemaSaxValidatorWithErrorHandler \ dictionary.xsd dictionary_invalid_xsd.xml Validator Class: com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 22; cvc-datatype-valid.1.2.1: 'yes' is not a valid value for 'boolean'. org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 22; cvc-attribute.3: The value 'yes' of attribute 'acronym' on element 'word' is not valid with respect to its type, 'boolean'. org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 31; cvc-pattern-valid: Value '23-Dec-2014' is not facet-valid with respect to pattern '\p{Nd}{4}-\p{Nd}{2}-\p{Nd}{2}' for type '#AnonType_dateupdateType'. org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 31; cvc-attribute.3: The value '23-Dec-2014' of attribute 'date' on element 'update' is not valid with respect to its type, 'null'. org.xml.sax.SAXParseException; lineNumber: 20; columnNumber: 33; cvc-complex-type.3.2.2: Attribute 'editor' is not allowed to appear in element 'update'. org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 36; cvc-datatype-valid.1.2.1: 'no' is not a valid value for 'boolean'. org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 36; cvc-attribute.3: The value 'no' of attribute 'symbol' on element 'word' is not valid with respect to its type, 'boolean'. Failed with errors: 7
XsdSchemaSaxValidatorWithErrorHandler.java works. But XsdSchemaDomValidatorWithErrorHandler.java reports an extra error:
herong> java XsdSchemaDomValidatorWithErrorHandler \ dictionary.xsd dictionary_invalid_xsd.xml Validator Class: com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl org.xml.sax.SAXParseException; cvc-complex-type.3.2.2: Attribute 'xsi:noNamespaceSchemaLocation' is not allowed to appear in element 'dictionary'. org.xml.sax.SAXParseException; cvc-datatype-valid.1.2.1: 'yes' is not a valid value for 'boolean'. org.xml.sax.SAXParseException; cvc-attribute.3: The value 'yes' of attribute 'acronym' on element 'word' is not valid with respect to its type, 'boolean'. org.xml.sax.SAXParseException; cvc-pattern-valid: Value '23-Dec-2014' is not facet-valid with respect to pattern '\p{Nd}{4}-\p{Nd}{2}-\p{Nd}{2}' for type '#AnonType_dateupdateType'. org.xml.sax.SAXParseException; cvc-attribute.3: The value '23-Dec-2014' of attribute 'date' on element 'update' is not valid with respect to its type, 'null'. org.xml.sax.SAXParseException; cvc-complex-type.3.2.2: Attribute 'editor' is not allowed to appear in element 'update'. org.xml.sax.SAXParseException; cvc-datatype-valid.1.2.1: 'no' is not a valid value for 'boolean'. org.xml.sax.SAXParseException; cvc-attribute.3: The value 'no' of attribute 'symbol' on element 'word' is not valid with respect to its type, 'boolean'. Failed with errors: 8
The first error tells that 'xsi:noNamespaceSchemaLocation' is not allowed. I have no idea why.
Table of Contents
Introduction of XML (eXtensible Markup Language)
DOM (Document Object Model) Programming Interface
SAX (Simple API for XML) Programming Interface
DTD (Document Type Definition) Introduction
Validating an XML Document against the Specified DTD Document Type
XSD (XML Schema Definition) Introduction
►Validating XML Documents Against Specified XML Schemas
XSD Schema Validator on XML DOM Object
XSD Schema Validator on XML DOM Object - Errors
XSD Schema Validator on XML SAX Object
►SAXParseException - 'xsi:noNamespaceSchemaLocation' Not Allowed
Validating XML Linked with XSD using XMLReader
XSL (Extensible Stylesheet Language) Introduction
XSLT (XSL Transformations) Introduction
XSLT Elements as Programming Statements
Control and Generate XML Element in the Result
PHP Extensions for XML Manipulation
Processing XML with Python Scripts
XML Tools Plugin for Notepad++
XML Plugin Packages for Atom Editor