java XSD 验证:此解析器不支持规范“null”版本“null”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12994494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:05:00  来源:igfitidea点击:

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

javaxsd

提问by peter.murray.rust

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

我正在尝试使用标准 Java 库验证 XML 文件并收到上述错误。我的 XSD 文件test1.xsd

<?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):

使用代码(在 Eclipse 中作为 Junit 测试运行):

@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 postbut 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).

这个错误似乎是由于与 XML 解析器(例如 Xerces)的不兼容引起的,请参阅这篇文章,但我没有框架(Eclipse 和 Junit 除外)。我的 POM 中没有明确的 xerces。是否有一个简单的解决方法(我不介意我使用什么解析器,只要它通过验证)。

回答by peter.murray.rust

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

我将其追溯到其他软件中包含的不兼容版本的 Xerces。强制使用更高版本的 Xerces 可以解决问题。

回答by Yogendra Singh

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

我认为您的 XSD 文件无法通过InputStream SCHEMA_STREAM = getClass().getResourceAsStream(SCHEMA_PATH);. 无障碍。

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

XSD仅使用如下更改的行验证您的代码和文件。它在JDK 1.6和 中运行没有任何问题JDK 1.7

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

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

当文件无法访问时,我得到的NullPointerException有点不同,如下所示:

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

回答by Ozgur

Did you try this in xsd?

你在 xsd 中试过这个吗?

For attributes use the attribute use="optional"

对于属性使用属性 use="optional"

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

对于元素使用属性nillable="true"或使用属性minOccurs="0"

Representations of null in XML Schema

XML Schema 中 null 的表示