簡體   English   中英

Woodstox/XML1.1/XSD 解析+驗證和 XInclude

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

請幫助我在下面使用我的 Java/woodstox 代碼。 我還在示例中提供了一個 xsd 和兩個 xml 文件。

主要問題

我打開了驗證並希望出現驗證錯誤,因為

  • ID foo1 和 foo2 在 test2.xml 中定義了兩次,
  • ID foo 在 test2.xml 中沒有定義(除非我希望它使用 XInclude 發生,因此考慮了 test1.xml 中的 ID),並且
  • ID foo3 在 test2.xml 中沒有定義就被使用。 但是,沒有顯示驗證問題。

測試.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>

測試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>

測試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代碼

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();
}

我嘗試過的

我應該更好地使用SAXParserFactory saxParserFactory = WstxSAXParserFactory.newInstance(); 而不是XMLInputFactory xmlInputFactory = XMLInputFactory2.newInstance(); 作為第一步? 有什么區別?

然而,我在設置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"); 其中SAXNotSupportedException “不支持指定的架構語言。” 結果。

至少在那里我可以使用xmlReader.setErrorHandler(new SimpleErrorHandler()); 安裝一個錯誤處理程序,我在上面的代碼中沒有這樣做。

插件問題1

什么對我更好: createXMLStreamReadercreateXMLEventReader

插件問題2

我需要調整我的 XSD/XML 文件嗎? 尤其是頭條?

插件問題3

我需要在解析/驗證之前解析 Xincludes 嗎? 如果是這樣,如何?

進一步的背景

  • 顯然,代碼處於早期階段,我不太關心它如何結束。
  • 我使用 XML1.1 是因為我需要具有多個 ID 屬性的 xml 標簽。
  • 我使用 XInclude 是因為我想以模塊化方式定義我的 xml 文件以避免 xml 代碼重復。
  • Intellij 不驗證我的文件,所以我在此嘗試更深入地挖掘,但我認為到目前為止這些問題是無關的,因為在這里我沒有遇到驗證問題,而我在另一個線程中遇到了一個問題
  • 我將(幾乎相同的問題)發布到 Woodstox 郵件列表,但幾乎沒有任何活動。

幾點:

  1. 當您說 XML 1.1 時,我認為您指的是 XSD 1.1。

  2. 您的 Woodstox 代碼不會嘗試啟用架構驗證。

  3. 將 .xsd 文件擴展名用於普通 XML 實例文件而不是模式文檔是令人困惑的(但實際上並非不正確)。

  4. 我不知道是否有任何方法可以使用 Woodstox 啟用架構驗證(尤其是 XSD 1.1 驗證)。 除了使用 Saxon 驗證器,它允許模式驗證器的輸入是 StAXSource。

  5. Java 世界中有兩種 XSD 1.1 處理器可用:Xerces 和 Saxon。 如果您打算使用 Xerces 模式驗證器,我認為您可能需要將它與 Xerces XML 解析器一起使用(可能可以將它們解耦,但我不知道您為什么要這樣做)。 如果您選擇 Saxon XSD 處理器,那么它可以與任何 SAX 或 StAX 解析器一起使用,但我認為使用 StAX(即 Woodstox)沒有任何好處,因為 XSD 處理器位於推送管道上,這使得 SAX 更好合身。

  6. 至於XInclude處理,我想你可能想在XSD驗證之前做XInclude處理? 當您使用 Xerces 作為 XML 解析器和 Saxon 作為模式驗證器時,這很容易。 其他產品組合也有可能,我不確定。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM