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"
题
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"
解决方案
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"
其他提示
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