Java 获取给定XSD类型的所有XML节点
我想获取给定XSD类型的所有XML节点 例如(请参见下面的代码片段)Java 获取给定XSD类型的所有XML节点,java,xml,xpath,xsd,Java,Xml,Xpath,Xsd,我想获取给定XSD类型的所有XML节点 例如(请参见下面的代码片段) 对于XSD类型ListA,它应该只找到1个节点-MyLists/MyListA 对于XSD类型ItemType,它应该找到4个节点-2x MyLists/MyListA/ItemA和2x MyLists/MyListB/ItemB,但不是MyLists/MyListC/内部的节点,因为它们是CustomItemType的类型(尽管它们有相同的名称-类型不同) 是否有java库可以提供此功能 或者有没有办法手动解决这个问题?
- 对于XSD类型ListA,它应该只找到1个节点-MyLists/MyListA
- 对于XSD类型ItemType,它应该找到4个节点-2x MyLists/MyListA/ItemA和2x MyLists/MyListB/ItemB,但不是MyLists/MyListC/内部的节点,因为它们是CustomItemType的类型(尽管它们有相同的名称-类型不同)
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<xs:complexType name="ListA">
<xs:sequence>
<xs:element name="ItemA" type="ItemType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ListB">
<xs:sequence>
<xs:element name="ItemB" type="ItemType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="AnotherList">
<xs:sequence>
<xs:element name="ItemA" type="CustomItemType" maxOccurs="unbounded"/>
<xs:element name="ItemB" type="CustomItemType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ItemType">
<xs:sequence>
<xs:element name="ID" type="xs:string" />
<xs:element name="Value" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="CustomItemType">
<xs:sequence>
<xs:element name="ID" type="xs:string" />
<xs:element name="Value" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:element name="MyLists">
<xs:complexType>
<xs:sequence>
<xs:element name="MyListA" type="ListA" />
<xs:element name="MyListB" type="ListB" />
<xs:element name="MyListC" type="AnotherList" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<MyLists>
<MyListA>
<ItemA>
<ID>1</ID>
<Value>A1</Value>
</ItemA>
<ItemA>
<ID>2</ID>
<Value>A2</Value>
</ItemA>
</MyListA>
<MyListB>
<ItemB>
<ID>1</ID>
<Value>B1</Value>
</ItemB>
<ItemB>
<ID>2</ID>
<Value>B2</Value>
</ItemB>
</MyListB>
<MyListC>
<ItemA>
<ID>1</ID>
<Value>A1</Value>
</ItemA>
<ItemB>
<ID>2</ID>
<Value>B1</Value>
</ItemB>
</MyListC>
</MyLists>
XML示例
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<xs:complexType name="ListA">
<xs:sequence>
<xs:element name="ItemA" type="ItemType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ListB">
<xs:sequence>
<xs:element name="ItemB" type="ItemType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="AnotherList">
<xs:sequence>
<xs:element name="ItemA" type="CustomItemType" maxOccurs="unbounded"/>
<xs:element name="ItemB" type="CustomItemType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ItemType">
<xs:sequence>
<xs:element name="ID" type="xs:string" />
<xs:element name="Value" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="CustomItemType">
<xs:sequence>
<xs:element name="ID" type="xs:string" />
<xs:element name="Value" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:element name="MyLists">
<xs:complexType>
<xs:sequence>
<xs:element name="MyListA" type="ListA" />
<xs:element name="MyListB" type="ListB" />
<xs:element name="MyListC" type="AnotherList" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<MyLists>
<MyListA>
<ItemA>
<ID>1</ID>
<Value>A1</Value>
</ItemA>
<ItemA>
<ID>2</ID>
<Value>A2</Value>
</ItemA>
</MyListA>
<MyListB>
<ItemB>
<ID>1</ID>
<Value>B1</Value>
</ItemB>
<ItemB>
<ID>2</ID>
<Value>B2</Value>
</ItemB>
</MyListB>
<MyListC>
<ItemA>
<ID>1</ID>
<Value>A1</Value>
</ItemA>
<ItemB>
<ID>2</ID>
<Value>B1</Value>
</ItemB>
</MyListC>
</MyLists>
1.
A1
2.
A2
1.
地下一层
2.
地下二层
1.
A1
2.
地下一层
您可以通过使用类似于//元素(*,YourGlobalTypeName)
()的测试,使用支持模式的XPath 2.0或更高版本或支持模式的XQuery 1.0或更高版本来解决这一问题,因此对于您的示例,测试//元素(*,ListA)
返回一个元素而//元素(*,ItemType)
返回四个元素。在Java世界中,支持模式的XPath 2.0/3.0/3.1和XQuery 1.0/3.0/3.1由Saxon 9 EE支持,还有各种各样的XQuery实现,如exist db或basex,但我不确定它们是否支持支持支持模式感知的XQuery。您可以使用模式感知的XPath 2.0或更高版本,或模式感知的XQuery 1.0或更高版本,通过使用类似于//元素(*,YourGlobalTypeName)
()的测试来解决这一问题,因此在您的示例中使用test//元素(*,ListA)
返回一个元素,//元素(*,ItemType)
返回四个元素。在Java世界模式感知XPath 2.0/3.0/3.1和XQuery 1.0/3.0/3.1中,Saxon 9 EE支持XQuery 1.0/3.0/3.1,也有各种XQuery实现,如exist db或basex,但我不确定它们是否支持模式感知XQuery。一种手动方法是使用XSVisitor
解析xml文档同时使用ument和模式。这里有一个使用XSVisitor的示例,您可以根据您的用例进行调整一个手动方法是使用XSVisitor
同时解析xml文档和模式。这里有一个使用XSVisitor的示例,您可以根据您的用例进行调整,因为Saxon 9 EE需要许可证模式感知XPath 2.0,我使用了Eclipse PsychoPath-它只支持XPath 2.0,不支持XQuery,但对我的项目来说已经足够好了。-因为Saxon 9 EE需要模式感知XPath 2.0的许可证,所以我使用了Eclipse PsychoPath-它只支持XPath 2.0,不支持XQuery,但对我的项目来说已经足够好了-