XPath基于两个相关的子项来标识父项

时间:2015-06-18 19:20:54

标签: xpath

使用以下示例,我尝试创建一个XPath,它将标识其branchID与分支ID匹配的App的ID,但treeID与分支的treeID不匹配。

例如:// growth [@ type =“apple”] [branchID = // branch / @ id] / @ id - 结果将是 奶奶 帝国 加拉

并且//增长[@type =“apple”] [treeID!= // branch / treeID] / @ id - 结果将是 奶奶 帝国 晚会

但我想要一个只会返回的查询: 奶奶

<xml>
  <growth type="apple" id="Granny">
   <branchID>ABCD</branchID>
   <treeID>456</treeID>
  </growth>
  <growth type="apple" id="Empire">
   <branchID>ABCD</branchID>
   <treeID>123</treeID>
  </growth>
  <growth type="apple" id="Gala">
   <branchID>EFGH</branchID>
   <treeID>456</treeID>
  </growth>
  <growth type="flower" id="white">
   <branchID>EFGH</branchID>
   <treeID>123</treeID>
  </growth>
  <growth type="flower" id="yellow">
   <branchID>ABCD</branchID>
   <treeID>456</treeID>
  </growth>
  <branch id="ABCD">
   <treeID>123</treeID>
  </branch>
  <branch id="EFGH">
    <treeID>456</treeID>
  </branch>
  <tree id="123" />
  <tree id="456" />
  <tree id="789" />
 </xml>

1 个答案:

答案 0 :(得分:1)

因此,所需的项目(其中branchID / treeID对不存在于分支元素中)将在所有// branch / @ id值的列表中具有不同的branchID索引值,并且所有// branch / treeID值的列表...

//growth[@type="apple"][index-of(//branch/@id,branchID)!=index-of(//branch/treeID,treeID)]/@id

Granny

在星期五的午餐时间给小灰细胞做一些运动的好问题:o)

相关问题