2015-07-11 77 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