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 使用XPath查找节点中的最后一行_Ruby_Xpath - Fatal编程技术网

Ruby 使用XPath查找节点中的最后一行

Ruby 使用XPath查找节点中的最后一行,ruby,xpath,Ruby,Xpath,我想知道是否有一种方法可以始终选择某个元素上方节点的内容 我想从以下代码中提取: <div id="someDiv"> <h3>Name</h3> Some content1 <br/> <br/> Address 12345 <br/> 09876 City, Country <br/> <span id="tel_number">12345&l

我想知道是否有一种方法可以始终选择某个元素上方节点的内容

我想从以下代码中提取:

<div id="someDiv">
   <h3>Name</h3>
   Some content1
   <br/>
   <br/>
   Address 12345
   <br/>
   09876 City, Country
   <br/>
   <span id="tel_number">12345</span>
</div>
现在,我需要的是一个XPath,它总是选择范围上方的内容,而不选择其他内容(一行)。如果(出于某种原因)跨度上方的

缺失,该功能也应起作用

希望有人能帮上忙

我想检索没有任何HTML标记的“09876城市,国家”

我认为你在寻找以下方面:

//div[@id="someDiv"]/span[@id="tel_number"]/preceding-sibling::text()[1]
使用Nokogiri:

require 'nokogiri'

doc = Nokogiri::HTML::Document.parse <<-EOT
<div id="someDiv">
   <h3>Name</h3>
   Some content1
   <br/>
   <br/>
   Address 12345
   <br/>
   09876 City, Country
   <br/>
   <span id="tel_number">12345</span>
</div>
EOT

doc.xpath("normalize-space(//div[@id='someDiv']/span[@id='tel_number']/preceding-sibling::text()[1])").to_s
# => "09876 City, Country"
需要“nokogiri”
doc=Nokogiri::HTML::Document.parse
我想检索没有任何HTML标记的“09876城市,国家”

我认为你在寻找以下方面:

//div[@id="someDiv"]/span[@id="tel_number"]/preceding-sibling::text()[1]
使用Nokogiri

require 'nokogiri'

doc = Nokogiri::HTML::Document.parse <<-EOT
<div id="someDiv">
   <h3>Name</h3>
   Some content1
   <br/>
   <br/>
   Address 12345
   <br/>
   09876 City, Country
   <br/>
   <span id="tel_number">12345</span>
</div>
EOT

doc.xpath("normalize-space(//div[@id='someDiv']/span[@id='tel_number']/preceding-sibling::text()[1])").to_s
# => "09876 City, Country"
需要“nokogiri”
doc=Nokogiri::HTML::Document.parse试试:

或者如果你想删除空白

normalize-space((//div[@id="someDiv"]/span[@id="tel_number"]/preceding-sibling::text())[last()])
尝试:

或者如果你想删除空白

normalize-space((//div[@id="someDiv"]/span[@id="tel_number"]/preceding-sibling::text())[last()])

我发现检索邮政编码的最佳方法如下:

data = page.search('(//div[@id="someDiv"]/span[@id="tel_number"]/preceding-sibling::node()').map{|data| data.text.cleanup}
data.delete("")
postcode = data.last.match(/\d{5}/).to_s

从这里可以很容易地检索选择之后或之前的所有内容。

我发现检索邮政编码的最佳方法如下:

data = page.search('(//div[@id="someDiv"]/span[@id="tel_number"]/preceding-sibling::node()').map{|data| data.text.cleanup}
data.delete("")
postcode = data.last.match(/\d{5}/).to_s

从这里可以轻松检索选择之后或之前的所有内容。

这只是返回元素本身吗?[3] 返回右行。但这是具体的方法。我正在寻找一种更通用的方法,比如使用[last()],只用于节点内的内容。@Severin ok。。你能告诉我你想要什么价值吗?只有文本还是元素?那还不清楚。。提到你正在寻找的输出。我想检索“09876 City,Country”,去掉任何HTML标记。那只返回元素本身?[3] 返回右行。但这是具体的方法。我正在寻找一种更通用的方法,比如使用[last()],只用于节点内的内容。@Severin ok。。你能告诉我你想要什么价值吗?只有文本还是元素?那还不清楚。。提到您正在寻找的输出。我想检索“09876 City,Country”,去掉任何HTML标记。在我的机器上,使用Python和lxml,而不是Ruby,它给出了
'09876 City,Country'
我也没有为此使用
Ruby
,我进行了测试。它的xpath计算简单而纯粹。(/[@id=“someDiv”]/span[@id=“tel\u number”]/previous sibling::text())[last()]似乎起到了作用。非常感谢。很抱歉,结果是(//div[@id=“someDiv”]/span[@id=“tel\u number”]/previous sibling::text())[last()]没有返回正确的内容。(//div[@id=“someDiv”]/span[@id=“tel\u number”]/previous sibling::text())[last()-3]可以,但这并不能解决问题。当我测试时,
(//div[@id=“someDiv”]/span[@id=“tel number”]/previous sibling::text())[last()]
正在工作(注意
[last()]前面的外括号)
;没有外括号,您可以使用@Babai的
//div[@id=“someDiv”]/span[@id=“tel\u number”]/previous sibling::text()[1]
。我没有使用Ruby进行测试。我不知道它是否依赖于XPath引擎。在我的机器上,使用Python和lxml,而不是Ruby,它给出了
'09876 City,Country'
我也没有使用
Ruby
,我进行了测试。它的计算既简单又纯粹。(/[@id=“someDiv”]/span[@id=“tel number”]/sibling::text())[last()]似乎很有用。谢谢!抱歉,但事实证明(//div[@id=“someDiv”]/span[@id=“tel\u number”]/previous sibling::text())[last()]并没有返回正确的结果。(//div[@id=“someDiv”]/span[@id=“tel\u number”]/previous sibling::text())[last()-3]确实返回了,但这并不能解决问题。当我在上测试时,
(//div id[@id>)=“someDiv”]/span[@id=“tel\u number”]/previous sibling::text())[last()]
正在工作(注意
[last()]
前面的外括号;如果没有外括号,您可以使用@Babai的
//div[@id=“someDiv”]/span[@id=“tel\u number”]/previous sibling::text()[1]
。我还没有使用Ruby进行测试。我不知道它是否依赖于XPath引擎。