Question

I have the next tags

    <svg><text id="text1" color="red">Hello!</text>
         <text id="text2" color="red">Another text </text></svg>

How can i do with Nokogiri to change the content between the tags, so i get

 <svg>   <text id="text1" color="red">Goodbye</text>
        <text id="text2" color="red">Another text </text></svg>

Is there a method like this??

document.at_xpath('//svg/text[@id="text1"]').text="Goodbye"
Was it helpful?

Solution

Do as below using Nokogiri::XML::Node#content=:

Set the Node’s content to a Text node containing string. The string gets XML escaped, not interpreted as markup.

document.at_xpath('//svg/text[@id="text1"]').content = "Goodbye"

OTHER TIPS

Selecting by id would be another solution, possibly more elegant for HTML:

require 'nokogiri'

html = %q{ 
  <html>
   <body>
    <svg><text id="text1" color="red">Hello!</text>
     <text id="text2" color="red">Another text </text></svg>   </body>
 </html>
}

doc = Nokogiri::XML(html)
hello = doc.at_css('#text1')
hello.content = "Goodbye"

puts doc
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top