Ruby on rails Nokogiri将标签分开放置
我想单独留言:Ruby on rails Nokogiri将标签分开放置,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,rubygems,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,Rubygems,我想单独留言: teste = Nokogiri::XML::DocumentFragment.parse("") Nokogiri::XML::Builder.with( teste ){ |x| x.exemplo "teste xml" } puts teste.to_xml 消息打印 <exemplo>teste xml</exemplo> teste xml 您想要的消息 <ns3:exemplo>t
teste = Nokogiri::XML::DocumentFragment.parse("")
Nokogiri::XML::Builder.with( teste ){ |x|
x.exemplo "teste xml"
}
puts teste.to_xml
消息打印
<exemplo>teste xml</exemplo>
teste xml
您想要的消息
<ns3:exemplo>teste</ns3:exemplo>
teste
以下是一个工作示例:
require 'nokogiri'
teste = Nokogiri::XML::DocumentFragment.parse("")
Nokogiri::XML::Builder.with(teste) do |x|
x.root('xmlns:ns3' => 'Example namespace') do
x['ns3'].example "Example Test"
end
end
puts teste.to_xml
请记住,必须先定义名称空间,然后才能使用。然后您使用
Nokogiri::XML::Builder#[]
定义一个名称空间,然后是正常的Nokogiri语法。尝试put teste.to_text
我很想告诉您想要的消息:teste非常感谢