Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby “@attr!”=&引用;“值”XPath中的'not(@attr=";value";)`_Ruby_Xpath_Nokogiri - Fatal编程技术网

Ruby “@attr!”=&引用;“值”XPath中的'not(@attr=";value";)`

Ruby “@attr!”=&引用;“值”XPath中的'not(@attr=";value";)`,ruby,xpath,nokogiri,Ruby,Xpath,Nokogiri,有一个像这样的HTML <div class="paginate_box"> <span class="disabled prev_page">Back</span> <span class="current">1</span> <a rel="next" href="page2">2</a> <a rel="next" href="page3">3</a> <a

有一个像这样的HTML

<div class="paginate_box">
  <span class="disabled prev_page">Back</span>
  <span class="current">1</span>
  <a rel="next" href="page2">2</a>
  <a rel="next" href="page3">3</a>
  <a class="next_page" rel="next" href="page2">Next</a>
</div>

起初我写了
a[@class!=“next_page”]
,而不是
a[not(@class=“next_page”)]
,但它与标记不匹配。为什么不匹配?我做错了什么?

所以这里的问题是您试图使用
=@class
)上的代码>。这意味着不能在其他节点上比较
@class
,因为它实际上什么也没说!='下一页

由于“无”不能与“任何”进行比较,运算符(包括
!=
=
)将始终返回false

not
函数中,您询问的是nothing='next_page',它总是
false
(如上所述),因此
not
使其
为true
,并选择元素

您可以通过将一个类添加到其他锚定标记之一,然后使用
来证明这一点=版本

旁注:您可以简化代码,只需使用
xpath

doc.xpath('//div[@class="paginate_box"]/a[not(@class="next_page")][last()]').text 
#=> "3"
# Or  
doc.xpath('//div[@class="paginate_box"]/a[not(@class="next_page")][last()]/text()').to_s
#=> "3"
此外,如果下一个页面锚定始终存在且始终是最后一个,并且最高页码始终在其前面,则您可以完全避免这种情况:

doc.xpath('//div[@class="paginate_box"]/a[position()=last()-1]').text
#=> "3"
这里我们说的是,在该div中最后一个锚点之前找到锚点

备选方案:

doc.xpath('//div[@class="paginate_box"]/a[last()]/preceding-sibling::a[1]').text
#=> "3"
这将找到最后一个锚点,然后按自下而上的顺序找到它前面的所有锚点兄弟,我们将在该列表中选择第一个锚点

doc.xpath('//div[@class="paginate_box"]/a[last()]/preceding-sibling::a[1]').text
#=> "3"