1

XPath 新手在这里。

有没有办法使用 XPath 从下面的示例 XML 中选择一个列表attr1和对?child3

换句话说,我需要一个a11, c13,a21, c23等的列表。

或者我只能//parent从结果节点数组中选择所需的值?

<parentList>
  <parent attr1="a11" attr2="a12">
    <child1>c11</child1>
    <child2>c12</child2>
    <child3>c13</child3>
    <child4>c14</child4>
    <child5>c15</child5>
  </parent>
  <parent attr1="a21" attr2="22">
    <child1>c21</child1>
    <child2>c22</child2>
    <child3>c23</child3>
    <child4>c24</child4>
    <child5>c25</child5>
  </parent>
</parentList>
4

2 回答 2

2

这个 xpath 2.0 表达式(并正确关闭您的示例 html)

//parent/concat(@attr1,' ',child3/text())

应该输出:

a11 c13
a21 c23
于 2020-06-19T16:55:35.903 回答
0

选择感兴趣的元素的 XPath 1.0 解决方案:

//parent/descendant-or-self::*[position()=1 or position()=4]

但是,要生成数据列表,您应该使用以下数据:

(//@attr1|//text()[normalize-space()])[parent::parent or parent::*[parent::parent][count(preceding-sibling::*)=2]]

查找具有 :parent元素作为父元素或子元素的属性或文本作为父元素parent和两个前面的兄弟元素。

输出 :a11,c13,a21,c23

于 2020-06-20T03:03:26.263 回答