Home » Java » XSD Validation: This parser does not support specification "null" version "null"

XSD Validation: This parser does not support specification "null" version "null"

Posted by: admin December 28, 2021 Leave a comment

Questions:

I am trying to validate XML files using standard Java libraries and get the above error. My XSD file test1.xsd is

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:complexType name="foo">
        <xsd:attribute name="bar" type="xsd:string" />
    </xsd:complexType>  
</xsd:schema>

with code (running as Junit test in Eclipse):

@Test
public void testValidatingParser1() throws Exception {
    String SCHEMA_PATH = "test1.xsd";
    InputStream SCHEMA_STREAM = getClass().getResourceAsStream(SCHEMA_PATH);
    StreamSource SCHEMA_SOURCE = new StreamSource(SCHEMA_STREAM);
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(SCHEMA_SOURCE);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setSchema(schema);
}

The error is:

java.lang.UnsupportedOperationException: This parser does not support specification "null" version "null"
at javax.xml.parsers.DocumentBuilderFactory.setSchema(Unknown Source)
at org.xmlcml.graphics.misc.SchemaTest.testValidatingParser1(SchemaTest.java:123)

This error seems to arise from incompatibilities with XML parsers (such as Xerces) see this post but I have no frameworks (other than Eclipse and Junit). I do not have xerces explicitly in my POM. Is there a simple workround (I don’t mind what parser I use as long as it validates).

Answers:

I tracked it down to incompatible versions of Xerces, included from other software. Forcing the later versions of Xerces cures the problem.

###

I think that your XSD file is not getting accessible through InputStream SCHEMA_STREAM = getClass().getResourceAsStream(SCHEMA_PATH);. accessible.

I validate the your code and XSD file with only changed line as below. It ran without any issues in JDK 1.6 and JDK 1.7.

         InputStream SCHEMA_STREAM = new FileInputStream(new File(SCHEMA_PATH));

When file was not accessible, I got the NullPointerException little different as below:

        Exception in thread "main" org.xml.sax.SAXParseException: 
          schema_reference.4: Failed to read schema document 'null'

###

Did you try this in xsd?

For attributes use the attribute use="optional"

For elements use the attribute nillable="true" or use the attribute minOccurs="0"

Representations of null in XML Schema