Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.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 使用Nokogiri添加节点_Ruby On Rails_Ruby_Nokogiri - Fatal编程技术网

Ruby on rails 使用Nokogiri添加节点

Ruby on rails 使用Nokogiri添加节点,ruby-on-rails,ruby,nokogiri,Ruby On Rails,Ruby,Nokogiri,我有一个HTML字符串,例如hello,我只想添加一个节点,如果字符串中的HTML标记是一个标签,例如Hi 我一生都无法理解我做错了什么,任何帮助都将不胜感激 编辑:这解决了我的问题,谢谢你的回答 # notice DocumentFragment rather than XML doc = Nokogiri::HTML::DocumentFragment.parse(html_tag) doc.children.each do |node| if node.name == 'label'

我有一个HTML字符串,例如hello,我只想添加一个节点,如果字符串中的HTML标记是一个标签,例如Hi

我一生都无法理解我做错了什么,任何帮助都将不胜感激

编辑:这解决了我的问题,谢谢你的回答

# notice DocumentFragment rather than XML
doc = Nokogiri::HTML::DocumentFragment.parse(html_tag)
doc.children.each do |node|
  if node.name == 'label'
    span = Nokogiri::XML::Node.new "span", doc
    node.add_child(span)
  end
end
一些问题-您的span=。。行正在创建节点,但实际上并没有将其添加到文档中。此外,您不能访问创建span的块之外的span

我想这就是你想要的:

html = '<label>Hi</label>'

doc = Nokogiri::XML(html)

doc.children.each do |node|
  if node.name == 'label'
    # this code gets called
    span = Nokogiri::XML::Node.new "span", doc
    span.content = "hello"
    node.add_child(span)
  end
end

# NOTE: `node` nor `span` are accessible outside of the each block

doc.to_s # => "<?xml version=\"1.0\"?>\n<label>Hi<span>hello</span></label>\n"

注意node.add_childspan行。

添加/更改/删除HTML很容易:

require 'nokogiri'

doc = Nokogiri::HTML::DocumentFragment.parse('<div class="input">hello</div>')
div = doc.at('div')
div << '<span>Hello</span>'
puts doc.to_html
其结果是:

# >> <div class="input">hello<span>Hello</span>
# >> </div>
# >> <div class="input"><span>Hello</span></div>
或:

Nokogiri使您可以在指向标签后轻松更改其名称。通过将{label.text}替换为所需的内容,可以轻松地更改中的文本


at'div label'是查找特定节点的一种方法。它基本上意味着在第一个div中查找第一个标签标签。at意味着查找某个内容的第一个,与使用search…first类似。如果需要的话,at和search中都有CSS和XPath等价物。

您希望新的span显示在哪里?您需要显示一个示例,说明输出/生成的HTML应该是什么样子。这就解决了这个问题,我使用的是Nokogiri::XML::当我应该使用Nokogiri::HTML::DocumentFragment::-使用DocumentFragment.parse和普通解析的区别在于,在后面的解析中,Nokogiri创建了一个带有周围标记的DOM,基本上是在进行修复,试图创建一个语法正确的文档。是否还要向片段添加父级?我需要在一个div中包装和iframe,并为该div指定一个特定的类。@ZackHerbert为此创建一个单独的问题。在评论中问一个新问题会抬高当前问题,不允许我们正确回答。请参见与链接页面一起提供的内容。关于Nokogiri的许多页面都回答了您的问题,所以在提问之前先搜索一下。是的,我很抱歉显示了混乱的代码。对span的puts调用实际上在块内部。
div.children = '<span>Hello</span>'
puts doc.to_html
# >> <div class="input"><span>Hello</span></div>
doc = Nokogiri::HTML::DocumentFragment.parse('<div class="input"><label>hello</label></div>')
label = doc.at('div label')
label.name = 'span' if label
puts doc.to_html
# >> <div class="input"><span>hello</span></div>
doc = Nokogiri::HTML::DocumentFragment.parse('<div class="input"><label>hello</label></div>')
label = doc.at('div label')
label.replace("<span>#{ label.text }</span>") if label
puts doc.to_html
# >> <div class="input"><span>hello</span></div>