Pedro Morte Rolo Pedro Morte Rolo - 8 months ago 33
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


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>"