Pedro Morte Rolo Pedro Morte Rolo - 1 year ago 95
Ruby Question

Nokogiri::XML::document canonicalize method returns empty string

I wanted to get a canonicalized version of a piece of xml by using nokogiri, though its canonicalize method is returnin an empty string.

d= '<a><z></z><b c="d">e</b><a>'
d.canonicalize #=> ""

Does anybody know what am I doing wrong? I am using Nokogiri 1.6.7

Answer Source

Document::new doesn’t actually parse the document. You are basically just creating an empty document with a rather odd XML version:

#=> "<?xml version='<a><z></z><b c=\"d\">e</b></a>'?>\n"

Instead use Document::parse, or the XML() method on the Nokogiri module:

d = Nokogiri::XML::Document.parse '<a><z></z><b c="d">e</b></a>'
d.canonicalize #=> "<a><z></z><b c=\"d\">e</b></a>"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download