Ruby Convert Single Quotes to Double Quotes in Xml

Ruby convert single quotes to double quotes in XML

As of Feb 2007 there's a supported way of determining the quoting character. The changes were merged into Ruby sources on Jul 2007 and should be available on all versions since 1.8.6-p110:

require 'rexml/document'

doc = REXML::Document.new
doc.context[:attribute_quote] = :quote # <-- Set double-quote as the attribute value delimiter

root = doc.add_element('root')
root.add_attribute('val', '123')

doc.write(STDOUT)

Running that yields:

$ ruby test.rb
<root val="123"/>
$

Quotes in XML. Single or double?

Both are legal. Choose one and stick with it. It doesn't matter.

From the spec:

AttValue       ::=      '"' ([^<&"] | Reference)* '"'
| "'" ([^<&'] | Reference)* "'"

Showing that both are valid, as is mixing the two styles within an element, per attribute (though I suggest being consistent within any single document/set of documents).

Can nokogiri use single quotes for attributes on saving xml?

It looks like the answer is no; not as the library is currently written, and maybe not at all. Tracing the call path for a node's serialization:

  • Nokogiri::XML::Node#to_s calls to_xml
  • Nokogiri::XML::Node#to_xml calls serialize (sets a few default options)
  • Nokogiri::XML::Node#serialize calls write_to
  • Nokogiri::XML::Node#write_to calls native_write_to
  • Nokogiri::XML::Node#native_write_to calls native_write_to, which looks like this:

&bsp;

def native_write_to(io, encoding, indent_string, options)
set_xml_indent_tree_output 1
set_xml_tree_indent_string indent_string
savectx = LibXML.xmlSaveToIO(IoCallbacks.writer(io), nil, nil, encoding, options)
LibXML.xmlSaveTree(savectx, cstruct)
LibXML.xmlSaveClose(savectx)
io
end

So, you are at the mercy of libxml at this point. Googling for libxml serialize single quote attributes does not immediately turn up any smoking guns.

I think you should file a feature request and see what sort of tenderlovin' you can get. :)

How to escape double quote for Ruby inside of XML

A double quote inside an XML attribute is written as "e; (or " or "). You'll have similar issues with single quotes too so you can't use those. However, you can use % as-is in an XML attribute so %|...|, %Q|...|, and %q|...| are available and they're as easy to read and type as quotes:

<root name="test" type="Node" action="{puts %|ROOT|}">
<leaf type="Node" decider="{print %|VAL1|; gets.chomp.to_i}" action="{puts %|ONE|}" />
<!-- ... -->
</root>

Choose whichever delimiters you find the easiest to type and read.

You can also use single quotes for your attributes in XML so you can have:

<leaf type='Node' decider='{print "VAL1"; gets.chomp.to_i}' ...

But then you'd have to use ' inside the attribute if you needed to include a single quote.

Alternatively, you could switch to elements instead of attributes:

<leaf type="Node">
<decider><![CDATA[
print "VAL1"
gets.chomp.to_i
]]></decider>
<action><![CDATA[
puts "ONE"
]]></decider>
</leaf>

but that's a bit verbose, ugly, and not as easy to work with as attributes (IMHO).

Interpolating an XML string containing double quotes and colons in Ruby

As suggested by Jörg, you'll have much better experience if you use one of the templating languages. I suggested ERB, because it's built-in.

xs = '<ns1: xmlns:ns1="http://example.com" attr="<%= Time.now %>">'

require 'erb'
ERB.new(xs).result(binding)
# => "<ns1: xmlns:ns1=\"http://example.com\" attr=\"2017-06-23 09:11:56 +0300\">"

Ruby Single versus Double Quotes

Strings are strings in ruby. They are all equal. The only difference is how you declare them (single quotes, double quotes, here-docs and maybe something else). Once you've got a string value in a variable, it doesn't matter how it was declared.

s1 = 'single quoted'
s1 # => "single quoted"

So, if your strings don't leave the rubyland (that is, you don't render them as javascript or whatever), you shouldn't worry about the quotes.

Response to edit 2

It seems that you have to build your json object in a more manual manner, rather than relying on default behaviour of Array#to_s

To emit ruby string as javascript single-quoted string literal you can do something like this:

<script type="text/javascript">
var s = '<%= ruby_string %>';
</script>

<%= %> construct will render passed string without quotes. You supply the quotes yourself.



Related Topics



Leave a reply



Submit