在Java中使用不同的名称在一个xpath.evalutaion调用中选择多个节点

时间:2017-03-20 13:02:42

标签: java xml xpath

我想在一个xpath.evalutaion调用中使用不同的名称选择多个节点。我只想出了如何选择一个具有相同名称的节点并获取其中的内容。在此示例中,我获得<rrm:indentifier>内容。

String linkXPath = "/ds:dataSource/ds:artifact/ds:traceability/ds:links/ds:Link/ds:content/rrm:identifier/text()";
InputSource source = new InputSource(rootServicesResponse.getEntity().getContent());
NodeList attribute = (NodeList)(xpath.evaluate(linkXPath, source, XPathConstants.NODESET));

XML

<ds:traceability>
  <ds:links>
    <ds:Link type="Decomposition">
      <rrm:title>Parent Of</rrm:title>
      <ds:linkType>
        https://localhost:9443/rm/types/_cyuacfNmEeatIKybawF-9Q
      </ds:linkType>
      <ds:isParentLink>true</ds:isParentLink>
      <ds:isChildLink>false</ds:isChildLink>
      <rrm:relation>
        https://localhost:9443/rm/resources/_qmYQMfNpEeatIKybawF-9Q
      </rrm:relation>
      <rrm:identifier>1</rrm:identifier>
      <ds:content>
        <rrm:title>test</rrm:title>
        <rrm:identifier>1</rrm:identifier>
        <rrm:description/>
        <rrm:format>Text</rrm:format>
        <ds:artifactFormat>Requirement</ds:artifactFormat>
      </ds:content>
    </ds:Link>
  <ds:Link type="Decomposition">
    <rrm:title>Parent Of</rrm:title>
    <ds:linkType>
        https://localhost:9443/rm/types/_cyuacfNmEeatIKybawF-9Q
    </ds:linkType>
    <ds:isParentLink>true</ds:isParentLink>
    <ds:isChildLink>false</ds:isChildLink>
    <rrm:relation>
      https://localhost:9443/rm/resources/_RQWuYQJXEeeQ67tlGaw8LA
    </rrm:relation>
    <rrm:identifier>12</rrm:identifier>
    <ds:content>
       <rrm:title>kest</rrm:title>
       <rrm:identifier>12</rrm:identifier>
       <rrm:description/>
       <rrm:format>Text</rrm:format>
       <ds:artifactFormat>Requirement</ds:artifactFormat>
    </ds:content>
   </ds:Link>
  </ds:links>
</ds:traceability>

有没有办法选择里面的每个元素,例如,每个<ds:content>

1 个答案:

答案 0 :(得分:1)

您可以使用*来引用任何名称的元素,例如:

//ds:Link/ds:content/*

或者,如果要在一个XPath中指定多个元素名称:

//ds:Link/ds:content/*[self::rrm:title|self::rrm:identifier|self::rrm:format]