Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.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 on rails Xpath.1不在rails中工作_Ruby On Rails_Ruby_Xpath_Rexml - Fatal编程技术网

Ruby on rails Xpath.1不在rails中工作

Ruby on rails Xpath.1不在rails中工作,ruby-on-rails,ruby,xpath,rexml,Ruby On Rails,Ruby,Xpath,Rexml,我的代码: require 'rexml/document' require 'xpath' doc = REXML::Document.new(xml) XPath.each(doc, "*/categoryName") { |element| puts element.text } 我试图获取对象xml,其中xml是一个xml字符串。。。并检索一些文本ie-我想要这些文本 我原以为上面的代码可以工作,但它给了我以下错误: undefined method `each' for XPath

我的代码:

require 'rexml/document'
require 'xpath'

doc = REXML::Document.new(xml)
XPath.each(doc, "*/categoryName") { |element| puts element.text }
我试图获取对象xml,其中xml是一个xml字符串。。。并检索一些文本ie-我想要这些文本

我原以为上面的代码可以工作,但它给了我以下错误:

undefined method `each' for XPath:Module

我认为您错过了
要求rexml/xpath
并尝试了。它会起作用的

require 'rexml/document'
require 'rexml/xpath'
doc = REXML::Document.new(xml)
REXML::XPath.each( doc, "*/category") { |element| puts element.text }
一个例子:

require 'rexml/document'
require 'rexml/xpath'

doc = REXML::Document.new("<p>some text <b>this is bold!</b> more text</p>")
REXML::XPath.each(doc, "*//b") { |element| puts element.text }
# >> this is bold!
需要“rexml/文档”
需要“rexml/xpath”
doc=REXML::Document.new(部分文本为粗体!更多文本

) REXML::XPath.each(doc,“*//b”){| element | put element.text} #>>这太大胆了!
我不确定您正在加载什么“xpath”库,但您不想要或不需要它。令人困惑的是,REXML的文档假定您通过
include REXML
已经“污染”了您的全局对象。由于您没有这样做,因此需要提供模块的完整路径:

require 'rexml/document'
doc = REXML::Document.new(xml)
REXML::XPath.each( doc, "*/category"){ |el| puts el.text }
改用:
doc=Nokogiri.XML(XML);放置doc.xpath('*/categoryName').map(&:text)