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

543 Views Asked by At

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= Nokogiri::XML::Document.new '<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

1

There are 1 best solutions below

0
On BEST ANSWER

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

d.to_xml
#=> "<?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>"