简体   繁体   English

使用Woodstox / Stax2编写XML时验证失败

[英]Validation fails when writing XML with Woodstox / Stax2

I'm having an issue with XML validation using an XSD schema with Woodstox and Stax2. 我在使用具有Woodstox和Stax2的XSD架构的XML验证时遇到问题。 Validation fails even though the XML data conforms to the schema. 即使XML数据符合架构,验证也会失败。

Surprisingly, the validation issue only occurs when writing XML (using XMLStreamWriter2), not when reading XML (using XMLStreamReader2). 令人惊讶的是,验证问题仅在编写 XML(使用XMLStreamWriter2)时发生,而不在读取 XML(使用XMLStreamReader2)时发生。

I've built a small example to reproduce and isolate the error. 我建立了一个小示例来重现和隔离错误。 Basically, it just reads XML from a file into an XMLStreamReader2 (validating with an XSD schema), then copy it to an XMLStreamWriter2 (also validating with the same XSD). 基本上,它只是从文件中读取XML到XMLStreamReader2(使用XSD模式验证),然后将其复制到XMLStreamWriter2(也使用相同的XSD验证)。

Now, this fails with a validation error from the writer . 现在,此操作失败,并出现writer的验证错误。 If I deactivate validation on the writer, everything goes smoothly and the writer delivers perfectly conform XML. 如果我取消对编写器的验证,一切都会顺利进行,编写器将提供完全符合标准的XML。

Here is the code: 这是代码:

import com.ctc.wstx.stax.WstxInputFactory;
import com.ctc.wstx.stax.WstxOutputFactory;
import org.codehaus.stax2.XMLStreamReader2;
import org.codehaus.stax2.XMLStreamWriter2;
import org.codehaus.stax2.validation.XMLValidationSchema;
import org.codehaus.stax2.validation.XMLValidationSchemaFactory;

import javax.xml.stream.XMLStreamException;
import java.io.InputStream;
import java.io.StringWriter;

public class Converter {

    public static void main(String... args) throws XMLStreamException {

        InputStream reader = Converter.class.getClassLoader().getResourceAsStream("test.xml");
        StringWriter writer = new StringWriter();

        XMLValidationSchema schema = XMLValidationSchemaFactory.newInstance(XMLValidationSchema.SCHEMA_ID_W3C_SCHEMA)
                .createSchema(Converter.class.getClassLoader().getResourceAsStream("schema.xsd"));


        XMLStreamReader2 xmlReader = (XMLStreamReader2) new WstxInputFactory().createXMLStreamReader(reader);
        xmlReader.validateAgainst(schema);

        XMLStreamWriter2 xmlWriter = (XMLStreamWriter2) new WstxOutputFactory().createXMLStreamWriter(writer);
        xmlWriter.validateAgainst(schema);

        xmlWriter.copyEventFromReader(xmlReader, false);

        while (xmlReader.hasNext()) {
            xmlReader.next();

            xmlWriter.copyEventFromReader(xmlReader, false);
        }

        System.out.println(writer.toString());
    }
}

Here is the XML: 这是XML:

<?xml version="1.0" encoding="UTF-8"?>
<JobStatus xsdVersion="NA">
    <Document>
        <DocumentId>1234567890</DocumentId>
    </Document>
    <Document>
        <DocumentId>1234567891</DocumentId>
    </Document>
</JobStatus>

And here is the schema: 这是模式:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="JobStatus">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Document" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="DocumentId" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:attribute name="xsdVersion" type="xs:string" use="required"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

And this all result in (with validation on the writer enabled): 这一切导致(启用对编写器的验证):

Exception in thread "main" com.ctc.wstx.exc.WstxValidationException: element "JobStatus" is missing "xsdVersion" attribute
        at [row,col {unknown-source}]: [1,66]
        at com.ctc.wstx.exc.WstxValidationException.create(WstxValidationException.java:50)
        at com.ctc.wstx.sw.BaseStreamWriter.reportProblem(BaseStreamWriter.java:1223)
        at com.ctc.wstx.msv.GenericMsvValidator.reportError(GenericMsvValidator.java:549)
        at com.ctc.wstx.msv.GenericMsvValidator.reportError(GenericMsvValidator.java:541)
        at com.ctc.wstx.msv.GenericMsvValidator.reportError(GenericMsvValidator.java:535)
        at com.ctc.wstx.msv.GenericMsvValidator.validateElementAndAttributes(GenericMsvValidator.java:343)
        at com.ctc.wstx.sw.BaseNsStreamWriter.closeStartElement(BaseNsStreamWriter.java:420)
        at com.ctc.wstx.sw.BaseStreamWriter.copyEventFromReader(BaseStreamWriter.java:807)
        at Converter.main(Converter.java:34)

Without validation on the writer, the program runs fine and returns the same XML provided as input (modulo some indentation and line break differences) 无需在编写器上进行验证,该程序即可正常运行,并返回作为输入提供的相同XML(对某些缩进和换行符进行取模)

So my question is: am I doing something wrong with Woodstox here? 所以我的问题是:我在这里用伍德斯托克斯做错什么了吗? Why does validation fail only on the writer ? 为什么验证仅对编写者失败?

I can reproduce this issue with other pairs of XSD and XML, in which case you can get different kind of errors, but always on writer side. 我可以使用其他XSD和XML对重现此问题,在这种情况下,您可能会遇到其他类型的错误,但总是在编写者方面。 Validation on the reader side always work (as long as the XML conforms to the XSD obviously). 读者方的验证始终有效(只要XML明显符合XSD)。

Any insights would be greatly appreciated ! 任何见解将不胜感激!

PS: for reference, here are the dependencies and version the example uses PS:供参考,以下是示例使用的依赖项和版本

  • org.codehaus.woodstox stax2-api 4.0.0 org.codehaus.woodstox stax2-api 4.0.0
  • com.fasterxml.woodstox woodstox-core 5.0.2 com.fasterxml.woodstox woodstox-core 5.0.2
  • net.java.dev.msv msv-core 2013.6.1 net.java.dev.msv msv核心2013.6.1
  • net.java.dev.msv xsdlib 2013.6.1< net.java.dev.msv xsdlib 2013.6.1 <

It appears this was a bug in Woodstox when validating on write : https://github.com/FasterXML/woodstox/issues/16 似乎在写入验证时这是Woodstox中的错误: https : //github.com/FasterXML/woodstox/issues/16

The bug is now fixed as of release 5.0.3 of Woodstox, however there are still some issues with validation on write (see https://github.com/FasterXML/woodstox/issues/23 ). 该错误现在已从Woodstox的5.0.3版本中修复,但是在写入时仍然存在一些验证问题(请参阅https://github.com/FasterXML/woodstox/issues/23 )。

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

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