Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.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

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_Nokogiri - Fatal编程技术网

Ruby 我能否仅通过XPath获取特定属性的值?

Ruby 我能否仅通过XPath获取特定属性的值?,ruby,xpath,nokogiri,Ruby,Xpath,Nokogiri,我有这样一个代码: doc = Nokogiri::HTML("<a href='foo.html'>foo</a><a href='bar.html'>bar</a>") doc.xpath('//a/@href').map(&:value) # => ["foo.html", "bar.html"] doc=Nokogiri::HTML(“”) doc.xpath('//a/@href').map(&:value)#=>[“fo

我有这样一个代码:

doc = Nokogiri::HTML("<a href='foo.html'>foo</a><a href='bar.html'>bar</a>")
doc.xpath('//a/@href').map(&:value) # => ["foo.html", "bar.html"]
doc=Nokogiri::HTML(“”)
doc.xpath('//a/@href').map(&:value)#=>[“foo.html”、“bar.html”]
正如我所预料的那样


但出于好奇,我想知道,我是否可以仅通过使用XPath获得
href
属性的值?

从第一个答案判断,答案似乎是肯定和否定的。它提供了

xml.xpath("//Placement").attr("messageId")

这与“仅XPath”非常接近,但并不完全相同。由您来判断这是否足够。

首先定位属性

例如: 网站名称:

我们想找到“MTS”链接

在您的情况下,要定位此元素,我们可以使用x路径,如: //a[contains(text(),'MTS')]

现在要获取href属性,请使用:
//a[contains(text(),'MTS')]/@href

仅使用XPath
是什么意思?此
//a/@href
XPath
以获取
href
属性值。我想可能有一个xpath类似于
//a/@href/value()
。似乎没有。谢谢