1

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

Pedro Rolo
  • 28,273
  • 12
  • 60
  • 94
  • 1
    I disagree with the way this question was closed. The library is not providing a proper error and the original typo was not the cause for the mentioned problem. The answer matt provided is correct an helpful. Closing this question was abusive moderation. – Pedro Rolo Sep 09 '16 at 09:15

1 Answers1

1

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>"
matt
  • 78,533
  • 8
  • 163
  • 197