Ashley Raiteri
Ashley Raiteri

Reputation: 710

How to disable UTF character (punctuation) escaping when creating XML using default to_xml with Rails?

Given a rails models column that contains "Something & Something Else" when outputting to_xml Rails will escape the Ampersand like so:

<MyElement>Something &amp; Something Else</MyElement>

Our client software is all UTF aware and it would be better if we can just leave the column content raw in our XML output.

There was an old solution that worked by setting $KCODE="UTF8" in an environment file, but this trick no longer works, and was always an All or Nothing solution.

Any recommendations on how to disable this? on a case by case basis?

Upvotes: 1

Views: 450

Answers (2)

Michael Kay
Michael Kay

Reputation: 163468

This is nothing to do with Unicode (or "UTF"). Ampersands in XML must be escaped, otherwise it isn't XML, and no XML software will accept it. If you're saying you want the escaping disabled, then you're saying you don't want the output to be XML.

Upvotes: 0

mzjn
mzjn

Reputation: 51002

It does not matter if the client software is UTF-8-aware. An ampersand cannot be used unescaped in XML. If the software is supposed to also be XML-aware, then any content that includes ampersands is not allowed to be kept "raw".

Upvotes: 3

Related Questions