XPath - 查找具有特定属性值的最低元素

时间:2013-08-18 14:05:49

标签: xml xpath conditional-statements

如何获得适合某种条件的元素列表,但是也没有符合相同条件的死者(即其层次结构中符合条件的最低元素)?

例如,请考虑以下XML:

<root>
  <parent1 MyCond='true'>
    <child1 MyCond='false'>
      ...
    </child1>
    <child2 MyCond='true'>
      ...
    </child2>
    <child3 MyCond='false'>
      ...
    </child3>
  </parent1>
  <parent2 MyCond='false'>
    <child1 MyCond='false'>
      ...
    </child1>
    <child2 MyCond='true'>
      ...
    </child2>
  </parent2>
  <parent3 MyCond='true'>
    <child1 MyCond='false'>
      ...
    </child1>
  </parent3>
</root>

使用以下XPath表达式://*[@MyCond='true']提供[parent1, parent1/child2, parent2/child2, parent3]

我正在寻找一种过滤掉parent1的方法,该parent1/child2已经在列表中已有{{1}}。

1 个答案:

答案 0 :(得分:2)

只需添加谓词:没有具有真实条件的后代。

//*[@MyCond='true'][not(descendant::*[@MyCond='true'])] 

或等同地

//*[@MyCond='true' and not(descendant::*[@MyCond='true'])] 
相关问题