2015-04-05 118 views
3

假設下面的標記所有空元素:如何使用XPath選擇

<html> 
    <body> 
    <p> 
     <strong> </strong> 
     <strong> 
     </strong> 
     <strong><em>Bar</em> </strong> 
     <strong><em> </em> </strong> 
    </p> 
    </body> 
</html> 

我怎樣才能用XPath查詢以下元素?

<strong> </strong> 
<strong> 
</strong> 
<strong><em> </em> </strong> 

我以爲它會像//*[normalize-space(text()) = '' and not(node())]的第一兩種情況,但事實並非如此。而且我不知道如何捕捉第三種情況。

更確切地說:我正在搜索所有隻包含空白,新行和子節點的節點。

+0

http://stackoverflow.com/questions/8688185/xpath-select-tag-with-empty價值 請檢查以上職位。希望能幫助到你。 – TonalLynx 2015-04-05 15:08:36

回答

3

下面的XPath查詢捉住他們全部:

//*[not(normalize-space())] 

但不是:

<strong><em>Bar</em> </strong> 
+0

它也會捕獲'',這不在預期的結果列表中。 – 2015-12-21 22:58:53

+0

@MichaelKay它已經捕獲了列表中的父項,所以在這種情況下我不會太在意......但是如果你不想,我可以在我的問題中完成列表。 – witrin 2015-12-25 13:36:33