4

I use Nokogiri that would create XML. I want to have the following structure:

<content:encode>text</content>

I have tried this code:

xml.content['encoded'] {xml.text "text"}

but it gives me an error.

How do I write this correctly? A similar example is in Referencing declared namespaces.

1
  • 1
    Good job telling us what code you tried. In the future, you should always supply the exact error message you received, not just that you got an error. Commented Feb 16, 2011 at 13:59

1 Answer 1

11
  1. Your example doesn't make sense; you say that you want "encode" and then you attempt to write "encoded".

  2. Your example doesn't make sense, as it is not valid XML. You have an opening encode tag with the namespace content, and then you try to close it with a content tag. You want either <content:encode>text</content:encode> or you want <encode:content>text</encode:content>. (Which do you want?)

  3. You did not follow the example in the link you gave. If you want a content element with namespace encoded, then per the example you should write:

    xml['encoded'].content{ xml.text "text" }
    
  4. However, also per the example, you must declare any namespaces you want to reference. So do this:

    require 'nokogiri'
    
    builder = Nokogiri::XML::Builder.new do |xml|
      xml.root('xmlns:encoded' => 'bar') do
        xml['encoded'].content{ xml.text "text" }
      end
    end
    puts builder.to_xml
    #=> <?xml version="1.0"?>
    #=> <root xmlns:encoded="bar">
    #=>   <encoded:content>text</encoded:content>
    #=> </root>
    

Edit: If you really only need a single element with no root, using Nokogiri is overkill. Just do:

str = "Hello World"
xml = "<encoded:content>#{str}</encoded:content>"
puts xml
#=> <encoded:content>Hello World</encoded:content>

If you really need to use Nokogiri, but only want the first sub-root element, do:

xml_str = builder.doc.root.children.first.to_s
#=> "<encoded:content>text</encoded:content>"
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. But we need to get just such a structure. <encoded:content>text</encoded:content> <root xmlns:encoded="bar"> .... </root> - extra lines, they are not needed
@Delaf I do not understand your comments. Did what I write not help you? Are you saying that you want/need no newlines in your Nokogiri XML output?
Your example-works. But it adds extra lines to the code pastie.org/1581058
@Delaf I've edited the answer to possibly address your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.