Rectangle 27 0

ruby on rails How can I use a colon : in a nokogiri node name?


<?xml version="1.0"?>
<root>
  <node:name/>
</root>
<root xmlns:node="http://example.com">
  <node:name/>
</root>
builder = Nokogiri::XML::Builder.new do |xml|
  xml.root do
    xml.send 'node:name'
  end
end

As I see it, you have three options:

I'm building a web-service around a pre-existing xml standard, so I don't have an option and must to use the predefined schema. You are correct in number 3, i'm starting to see how the namespace relates to the xsd documents I have but don't completely understand namespaces yet. My xml standard seems to be made with nodes from various different namespaces, 4 of them, 2 xmlns:xsi and xmlns:xs seem to reference w3.org so I think they are general and not specific to my standard.

In this case you're probable best avoiding using colons in your element names. An alternative could be to use - instead. If you did this you'd need to use method 2 above, but with xml.send 'node-name'. I include this option because you don't mention namespaces in your question, and colons are used in them (as method 1 shows) so you're safer not using colons to avoid any future problems.

In this case, you need to send the method named "node:name" to the xml block parameter. You can do this with the normal ruby send method:

Then you can declare the namespace and use the xml[] method:

Note
Rectangle 27 0

ruby on rails How can I use a colon : in a nokogiri node name?


builder = Nokogiri::XML::Builder.new do  |xml|
  xml.send("foo:bar") do
  end
end


?> puts builder.to_xml
<?xml version="1.0"?>
<foo:bar/>
Note