简体   繁体   English

Woodstox/XML1.1/XSD 解析+验证和 XInclude

[英]Woodstox/XML1.1/XSD Parsing+Validation and XInclude

please help me with my Java/woodstox code down below.请帮助我在下面使用我的 Java/woodstox 代码。 I also provide an xsd and two xml files in my example.我还在示例中提供了一个 xsd 和两个 xml 文件。

Main problem主要问题

I turned on validation and would expect a validation error because我打开了验证并希望出现验证错误,因为

  • the IDs foo1 as well as foo2 are defined twice in test2.xml, ID foo1 和 foo2 在 test2.xml 中定义了两次,
  • the ID foo is used without definition in test2.xml (unless the ID from test1.xml is taken into consideration as I would like it to happen using the XInclude), and ID foo 在 test2.xml 中没有定义(除非我希望它使用 XInclude 发生,因此考虑了 test1.xml 中的 ID),并且
  • the ID foo3 is used without definition in test2.xml. ID foo3 在 test2.xml 中没有定义就被使用。 However, no validation problem is shown.但是,没有显示验证问题。

test.xsd测试.xsd

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

    <xs:attributeGroup name="ATTRIBUTES_TYPE_node">
        <xs:attribute name="id1" type="xs:ID" use="required"/>
        <xs:attribute name="id2" type="xs:ID" use="required"/>
        <xs:attribute name="idref" type="xs:IDREF" use="optional"/>
    </xs:attributeGroup>

    <xs:complexType name="TYPE_node">
        <xs:attributeGroup ref="ATTRIBUTES_TYPE_node"/>
    </xs:complexType>

    <xs:complexType name="TYPE_root">
        <xs:sequence>
            <xs:element name="node" type="TYPE_node" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>

    <xs:element name="root" type="TYPE_root"/>

</xs:schema>

test1.xsd测试1.xsd

<?xml version="1.1" encoding="utf-8"?>
<root
        xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
        vc:minVersion="1.1"
        vc:noNamespaceSchemaLocation="test.xsd">
    <node id1="foo" id2="bar" idref="foo"/>
</root>

test2.xsd测试2.xsd

<?xml version="1.1" encoding="utf-8"?>
<root
        xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
        vc:minVersion="1.1"
        xmlns:xi="http://www.w3.org/2001/XInclude"
        vc:noNamespaceSchemaLocation="test.xsd">
    <xi:include href="test1.xml">
        <xi:fallback/>
    </xi:include>

    <node id1="foo1" id2="foo2" idref="foo"/>
    <node id1="foo1" id2="foo2" idref="foo3"/>
</root>

Java code Java代码

XMLInputFactory xmlInputFactory = XMLInputFactory2.newInstance();
xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
xmlInputFactory.setProperty(XMLInputFactory.IS_VALIDATING, true);
xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new FileReader("src/main/resources/test2.xml"));

try {
    xmlStreamReader.nextTag();
    xmlStreamReader.require(XMLStreamConstants.START_ELEMENT, null, "root");
    xmlStreamReader.nextTag();

    while (true) {
        if (xmlStreamReader.getEventType() == XMLStreamConstants.START_ELEMENT)
            for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) {
                System.out.println("getAttributePrefix=" + xmlStreamReader.getAttributePrefix(i));
                System.out.println("getAttributeLocalName=" + xmlStreamReader.getAttributeLocalName(i));
                System.out.println("getAttributeName=" + xmlStreamReader.getAttributeName(i));
                System.out.println("getAttributeNamespace=" + xmlStreamReader.getAttributeNamespace(i));
                System.out.println("getAttributeType=" + xmlStreamReader.getAttributeType(i));
                System.out.println("getAttributeValue=" + xmlStreamReader.getAttributeValue(i));
            }
        xmlStreamReader.next();
    }
} finally {
    xmlStreamReader.close();
}

What I tried instead我尝试过的

Should I better use SAXParserFactory saxParserFactory = WstxSAXParserFactory.newInstance();我应该更好地使用SAXParserFactory saxParserFactory = WstxSAXParserFactory.newInstance(); instead of XMLInputFactory xmlInputFactory = XMLInputFactory2.newInstance();而不是XMLInputFactory xmlInputFactory = XMLInputFactory2.newInstance(); as a first step?作为第一步? What is the difference?有什么区别?

However, with this I ran into problems when setting saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage","http://www.w3.org/2007/XMLSchema-versioning");然而,我在设置saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage","http://www.w3.org/2007/XMLSchema-versioning");时遇到了问题saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage","http://www.w3.org/2007/XMLSchema-versioning"); where the SAXNotSupportedException "The specified schema language is not supported."其中SAXNotSupportedException “不支持指定的架构语言。” resulted.结果。

At least there I could use xmlReader.setErrorHandler(new SimpleErrorHandler());至少在那里我可以使用xmlReader.setErrorHandler(new SimpleErrorHandler()); to install an error handler, which I did not do in my code above.安装一个错误处理程序,我在上面的代码中没有这样做。

Addon Question1插件问题1

What is better for me: createXMLStreamReader or createXMLEventReader ?什么对我更好: createXMLStreamReadercreateXMLEventReader

Addon Question2插件问题2

Do I need to adjust my XSD/XML files?我需要调整我的 XSD/XML 文件吗? Especially the headers?尤其是头条?

Addon Question3插件问题3

Do I need to resolve the Xincludes before parsing/validation?我需要在解析/验证之前解析 Xincludes 吗? If so, how?如果是这样,如何?

Further Context进一步的背景

  • Clearly, the code is in an early stage where I do not bother much about how it ends.显然,代码处于早期阶段,我不太关心它如何结束。
  • I use XML1.1 because I need xml-tags with more than one ID-attribute.我使用 XML1.1 是因为我需要具有多个 ID 属性的 xml 标签。
  • I use XInclude because I want to define my xml-files in a modular way to avoid xml-code duplications.我使用 XInclude 是因为我想以模块化方式定义我的 xml 文件以避免 xml 代码重复。
  • Intellij does no validate my files so I am hereby trying to dig a bit deeper but I assume that the problems are unrelated as of now because here I get no validation problem whereas I get one in the other thread Intellij 不验证我的文件,所以我在此尝试更深入地挖掘,但我认为到目前为止这些问题是无关的,因为在这里我没有遇到验证问题,而我在另一个线程中遇到了一个问题
  • I posted (almost the same question) to the Woodstox mailing list but there is almost no activity.我将(几乎相同的问题)发布到 Woodstox 邮件列表,但几乎没有任何活动。 thread线

A few points:几点:

  1. When you say XML 1.1, I think you mean XSD 1.1.当您说 XML 1.1 时,我认为您指的是 XSD 1.1。

  2. Your Woodstox code makes no attempt to enable schema validation.您的 Woodstox 代码不会尝试启用架构验证。

  3. It's confusing (but not actually incorrect) to use a .xsd file extension for files that are ordinary XML instance files, not schema documents.将 .xsd 文件扩展名用于普通 XML 实例文件而不是模式文档是令人困惑的(但实际上并非不正确)。

  4. I don't know if there is any way to enable schema validation (esp. XSD 1.1 validation) with Woodstox.我不知道是否有任何方法可以使用 Woodstox 启用架构验证(尤其是 XSD 1.1 验证)。 Except by using the Saxon validator, which allows the input to a schema validator to be a StAXSource.除了使用 Saxon 验证器,它允许模式验证器的输入是 StAXSource。

  5. There are two XSD 1.1 processors available in the Java world: Xerces and Saxon. Java 世界中有两种 XSD 1.1 处理器可用:Xerces 和 Saxon。 If you're going to use the Xerces schema validator, I think you probably need to use it with the Xerces XML parser (it might be possible to decouple them, but I don't know why you would want to).如果您打算使用 Xerces 模式验证器,我认为您可能需要将它与 Xerces XML 解析器一起使用(可能可以将它们解耦,但我不知道您为什么要这样做)。 If you choose the Saxon XSD processor, then it will work with any SAX or StAX parser, but I don't see any benefits in using StAX (ie. Woodstox) because the XSD processor sits on a push pipeline and that makes SAX a better fit.如果您选择 Saxon XSD 处理器,那么它可以与任何 SAX 或 StAX 解析器一起使用,但我认为使用 StAX(即 Woodstox)没有任何好处,因为 XSD 处理器位于推送管道上,这使得 SAX 更好合身。

  6. As regards XInclude processing, I think you probably want to do XInclude processing before XSD validation?至于XInclude处理,我想你可能想在XSD验证之前做XInclude处理? That's easy enough when you use Xerces as the XML parser and Saxon as the schema validator.当您使用 Xerces 作为 XML 解析器和 Saxon 作为模式验证器时,这很容易。 It might also be possible with other product combinations, I don't know for certain.其他产品组合也有可能,我不确定。

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

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