Ruby 如何在Nokogiri节点中创建子元素?

Ruby 如何在Nokogiri节点中创建子元素?,ruby,xml,ruby-on-rails-4,nokogiri,children,Ruby,Xml,Ruby On Rails 4,Nokogiri,Children,我在Nokogiri上使用Rails 4.2.7。创建子节点时遇到问题。我有以下代码 general = doc.xpath("//lomimscc:general") description = Nokogiri::XML::Node.new "lomimscc:description", doc string = Nokogiri::XML::Node.new "lomimscc:string", doc string.content = scenario.abstract string['

我在Nokogiri上使用Rails 4.2.7。创建子节点时遇到问题。我有以下代码

general = doc.xpath("//lomimscc:general")
description = Nokogiri::XML::Node.new "lomimscc:description", doc
string = Nokogiri::XML::Node.new "lomimscc:string", doc
string.content = scenario.abstract
string['language'] = 'en'
description << string
general << description
general=doc.xpath(“//loimscc:general”)
description=Nokogiri::XML::Node.new“lomimscc:description”,doc
string=Nokogiri::XML::Node.new“lomimscc:string”,doc
string.content=scenario.abstract
字符串['language']='en'
description演示了如何执行此操作,但简单的示例是:

require 'nokogiri'

doc = Nokogiri::XML('<root/>')
doc.at('root').add_child('<foo/>')
doc.to_xml # => "<?xml version=\"1.0\"?>\n<root>\n  <foo/>\n</root>\n"
需要“nokogiri”
doc=Nokogiri::XML(“”)
文档位于('root')。添加子项(“”)
doc.to_xml#=>“\n\n\n\n”
Nokogiri使使用包含要添加的标记或节点的字符串轻松构建节点

您应该能够轻松地在此基础上进行构建。

当我更改时

general = doc.xpath("//lomimscc:general")


然后,所有操作都可以创建子节点。

Read“”。演示问题的最小XML是什么?教程实际上没有说明这一点(截至今天)
general = doc.xpath("//lomimscc:general").first