在 Rails 中使用默认 to_xml 创建 XML 时如何禁用 UTF 字符(标点符号)转义?

发布于 2024-12-26 05:54:14 字数 363 浏览 1 评论 0原文

给定一个 Rails models 列,其中包含 输出 to_xml 时出现“Something & Something Else” Rails 将像这样转义 & 符号:

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

我们的客户端软件都是 UTF 识别的,如果我们可以在 XML 输出中保留原始的列内容,那就更好了。

有一个旧的解决方案,通过在环境文件中设置 $KCODE="UTF8" 来工作,但这个技巧不再有效,并且始终是一个全有或全无的解决方案。

关于如何禁用此功能有什么建议吗?视具体情况而定?

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

<MyElement>Something & 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫烠⑼条掵仅有一顆心 2025-01-02 05:54:14

客户端软件是否支持 UTF-8 并不重要。在 XML 中,& 符号不能以未转义的方式使用。如果软件还应该支持 XML,那么任何包含“&”符号的内容都不允许保持“原始”状态。

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".

稀香 2025-01-02 05:54:14

这与 Unicode(或“UTF”)无关。 XML 中的 & 符号必须进行转义,否则它就不是 XML,任何 XML 软件都不会接受它。如果您说您希望禁用转义,那么您就是说您不希望输出为 XML。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文