获取给定XSD类型的所有XML节点



我想获得给定XSD类型的所有XML节点。

例如(请参阅下面的代码段(

  • 对于XSD类型 Lista ,它应该只找到1个节点-MyLists/ mylista
  • 对于XSD类型 itemType ,它应该找到4个节点-2x mylists/mylista/mlista/ itema 和2x mylists/mylistb/mylistb/ itemb> itemb 但是不是myLists/mylistc/中的节点,因为它们是 customItemtype 的类型(尽管它们具有相同的名称 - 类型是不同的(。

是否有可以提供此功能的Java库?

或任何想法如何手动解决这个问题?XSD可能非常复杂,并带有其他模式,等等。我正在考虑通过穿越XSD模式(将没有递归(将所有可能的XPath生成带有给定类型的节点,然后将它们应用于XML文件,然后检查是否找到了一些节点。

XSD示例

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

XML示例

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

您可以使用//element(*, YourGlobalTypeName)(https://www.w3.org/tr/(使用schema-Aware XPath 2.0或更高版本或更高版本或更高版本XQUERY 1.0或更高版本解决该问题。xPath20/#prod-xpath-elementTest(,因此,使用样本,测试//element(*, ListA)返回一个元素,//element(*, ItemType)返回四个元素。在Java World架构中,XPath 2.0/3.0/3.1和Xquery 1.0/3.0/3.1得到Saxon 9 EE的支持,也有各种XQUERY实现,例如ANDY-DB或BASEX,但我不确定它们是否支持架构,Xquery。

最新更新