[英]Woodstox/XML1.1/XSD Parsing+Validation and XInclude
請幫助我在下面使用我的 Java/woodstox 代碼。 我還在示例中提供了一個 xsd 和兩個 xml 文件。
我打開了驗證並希望出現驗證錯誤,因為
<?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>
<?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>
<?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>
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());
安裝一個錯誤處理程序,我在上面的代碼中沒有這樣做。
什么對我更好: createXMLStreamReader
或createXMLEventReader
?
我需要調整我的 XSD/XML 文件嗎? 尤其是頭條?
我需要在解析/驗證之前解析 Xincludes 嗎? 如果是這樣,如何?
幾點:
當您說 XML 1.1 時,我認為您指的是 XSD 1.1。
您的 Woodstox 代碼不會嘗試啟用架構驗證。
將 .xsd 文件擴展名用於普通 XML 實例文件而不是模式文檔是令人困惑的(但實際上並非不正確)。
我不知道是否有任何方法可以使用 Woodstox 啟用架構驗證(尤其是 XSD 1.1 驗證)。 除了使用 Saxon 驗證器,它允許模式驗證器的輸入是 StAXSource。
Java 世界中有兩種 XSD 1.1 處理器可用:Xerces 和 Saxon。 如果您打算使用 Xerces 模式驗證器,我認為您可能需要將它與 Xerces XML 解析器一起使用(可能可以將它們解耦,但我不知道您為什么要這樣做)。 如果您選擇 Saxon XSD 處理器,那么它可以與任何 SAX 或 StAX 解析器一起使用,但我認為使用 StAX(即 Woodstox)沒有任何好處,因為 XSD 處理器位於推送管道上,這使得 SAX 更好合身。
至於XInclude處理,我想你可能想在XSD驗證之前做XInclude處理? 當您使用 Xerces 作為 XML 解析器和 Saxon 作為模式驗證器時,這很容易。 其他產品組合也有可能,我不確定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.