Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.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 为什么Nokogiri::CSS.xpath_for返回数组?_Ruby_Xpath_Css Selectors_Nokogiri - Fatal编程技术网

Ruby 为什么Nokogiri::CSS.xpath_for返回数组?

Ruby 为什么Nokogiri::CSS.xpath_for返回数组?,ruby,xpath,css-selectors,nokogiri,Ruby,Xpath,Css Selectors,Nokogiri,我正在查看Nokogiri::CSS的源代码,因为我需要将CSS选择器转换为XPATH。在运行示例代码并调用xpath_for方法之后,我看到它返回了一个数组。为什么会这样?CSS选择器是否有可能返回多个xpath [18] pry(main)> Nokogiri::CSS.xpath_for 'div.divddy input:first' => ["//div[contains(concat(' ', @class, ' '), ' divddy ')]//input[posit

我正在查看Nokogiri::CSS的源代码,因为我需要将CSS选择器转换为XPATH。在运行示例代码并调用xpath_for方法之后,我看到它返回了一个数组。为什么会这样?CSS选择器是否有可能返回多个xpath

[18] pry(main)> Nokogiri::CSS.xpath_for 'div.divddy input:first'
=> ["//div[contains(concat(' ', @class, ' '), ' divddy ')]//input[position() = 1]"]
A:

5。选择器组

以逗号分隔的选择器列表表示列表中每个选择器选择的所有元素的并集

例如:

>> Nokogiri::CSS.xpath_for 'div.divddy input:first, div#where_is input.pancakes_house'
=> ["//div[contains(concat(' ', @class, ' '), ' divddy ')]//input[position() = 1]", "//div[@id = 'where_is']//input[contains(concat(' ', @class, ' '), ' pancakes_house ')]"]
div.divddy输入:首先,div#where_是input.pancakes_house{/*…*/}
因此,假设返回一个数组,以防您将一个分组选择器交给它。例如:

>> Nokogiri::CSS.xpath_for 'div.divddy input:first, div#where_is input.pancakes_house'
=> ["//div[contains(concat(' ', @class, ' '), ' divddy ')]//input[position() = 1]", "//div[@id = 'where_is']//input[contains(concat(' ', @class, ' '), ' pancakes_house ')]"]

请注意,在这种情况下返回的数组的长度为2。

或者更简单地说:
Nokogiri::CSS.xpath_代表“a,b”#=>[“//a”,“//b”]
@Phrogz:是的,但是我不能插入Fargo引用;)回答得很好,非常感谢!强制性参考(OT)