2013-08-18 139 views
1

如何獲得適合某種條件的元素列表,但也沒有適合相同條件的元素(即其層次結構中的最低元素符合條件)?XPath - 找到具有特定屬性值的最低元素

例如,請考慮以下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已經在列表中。

回答

2

只需添加謂詞:不存在具有真實條件的後代。

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

,或者等價地,

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

謝謝,我會試試看! – Elist

相關問題