Newlines in attributes

102 views
Skip to first unread message

Avin Mathew

unread,
Jul 30, 2014, 8:22:15 AM7/30/14
to nokogi...@googlegroups.com
I'm trying to parse newlines in attributes, but it seems they are converted to spaces:

>> require 'nokogiri'
=> true
>> Nokogiri::XML("<root attr=\"value\n\with\nspaces\"/>").to_xml
=> "<?xml version=\"1.0\"?>\n<root attr=\"value with spaces\"/>\n"

I did come across this related post (https://www.ruby-forum.com/topic/361457), but I don't think the question was resolved. Newlines in attributes seem to be valid characters (http://stackoverflow.com/questions/449627/are-line-breaks-in-xml-attribute-values-valid).

Is this a bug or am I missing a parse option?

Keshav Prasad

unread,
Jul 30, 2014, 5:21:59 PM7/30/14
to nokogi...@googlegroups.com
Try using single quotes instead of double quotes. ie 

Nokogiri::XML('<root attr=\"value\n\with\nspaces\"/>').to_xml

--
You received this message because you are subscribed to the Google Groups "nokogiri-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nokogiri-tal...@googlegroups.com.
To post to this group, send email to nokogi...@googlegroups.com.
Visit this group at http://groups.google.com/group/nokogiri-talk.
For more options, visit https://groups.google.com/d/optout.

Avin Mathew

unread,
Jul 30, 2014, 7:16:17 PM7/30/14
to nokogi...@googlegroups.com
Using single quotes would just put a literal backslash and "n" character in the string, see http://en.wikibooks.org/wiki/Ruby_Programming/Strings#Single_quotes

Using a heredoc produces the same result:

>> require 'nokogiri'
=> true
>> xml = <<EOS
<root attr="value
with
newlines"/>
EOS
=> "<root attr=\"value\nwith\nnewlines\"/>\n"
>> Nokogiri::XML(xml).to_xml
=> "<?xml version=\"1.0\"?>\n<root attr=\"value with newlines\"/>\n"
Reply all
Reply to author
Forward
0 new messages