2015-07-11 74 views
1

我有以下的HTML結構,它包含幾個郵件列表,我想抓住電子郵件,其中電子郵件業務,而不是雅虎,Gmail,Hotmail等的XPath不包含某些值

<a href="#1">[email protected]</a> 
<a href="#2">[email protected]</a> 
<a href="#5">[email protected]</a> 
<a href="#3">[email protected]</a> 
<a href="#6">[email protected]</a> 
<a href="#4">[email protected]</a> 
所有元素文本

所以我想是

[email protected] 
[email protected] 

我的想法是

get A tag which NOT contain ymail AND NOT contain yahoo AND NOT contain gmail, AND NOT contain hotmail 

但我怎麼能寫的XPath SY根據上面的想法ntax?

回答

1

你的點子直接轉換成的XPath如下:

//a[not(contains(., 'ymail')) and not(contains(., 'yahoo')) and not(contains(., 'gmail')) and not(contains(., 'hotmail'))]/text() 

對於示例(具有添加一個根元素),

<html> 
<a href="#1">[email protected]</a> 
<a href="#2">[email protected]</a> 
<a href="#5">[email protected]</a> 
<a href="#3">[email protected]</a> 
<a href="#6">[email protected]</a> 
<a href="#4">[email protected]</a> 
</html> 

它選擇

[email protected] 
[email protected] 

的要求。

3

您可以使用substring-aftersubstring-before@後和前第一獲得的部分。notcontains

結合,從而substring-before(substring-after(text(),"@"),'.')會得到域的第一部分和//a[not(contains("ymail yahoo gmail hotmail", ...))]將排除你想要的人。

共有

//a[not(contains("ymail yahoo gmail hotmail", substring-before(substring-after(text(),"@"),'.')))] 
+0

不錯!我會離開我的文字編碼,但我更喜歡這個答案。以這種方式反轉'contains()'的參數是非常聰明的。 – kjhughes