简体   繁体   English

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

[英]XSD Validation: This parser does not support specification “null” version “null”

I am trying to validate XML files using standard Java libraries and get the above error. 我正在尝试使用标准Java库验证XML文件并获得上述错误。 My XSD file test1.xsd is 我的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 post but I have no frameworks (other than Eclipse and Junit). 这个错误似乎从XML解析器的不兼容性(如Xerces的),以引起看到这个帖子 ,但我没有框架(比在Eclipse上和JUnit等)。 I do not have xerces explicitly in my POM. 我的POM中没有明确的xerces。 Is there a simple workround (I don't mind what parser I use as long as it validates). 有一个简单的workround(我不介意我使用什么解析器,只要它验证)。

I tracked it down to incompatible versions of Xerces, included from other software. 我将其跟踪到Xerces的不兼容版本,包含在其他软件中。 Forcing the later versions of Xerces cures the problem. 强制Xerces的更高版本解决了这个问题。

I think that your XSD file is not getting accessible through InputStream SCHEMA_STREAM = getClass().getResourceAsStream(SCHEMA_PATH); 我认为您的XSD文件无法通过InputStream SCHEMA_STREAM = getClass().getResourceAsStream(SCHEMA_PATH);访问InputStream SCHEMA_STREAM = getClass().getResourceAsStream(SCHEMA_PATH); . accessible. 无障碍。

I validate the your code and XSD file with only changed line as below. 我只使用更改的行验证您的代码和XSD文件,如下所示。 It ran without any issues in JDK 1.6 and JDK 1.7 . 它在JDK 1.6JDK 1.7没有任何问题。

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

When file was not accessible, I got the NullPointerException little different as below: 当文件无法访问时,我得到的NullPointerException有点不同,如下所示:

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

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表示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM