Ruby Builder - XML 输出对 HTML 实体进行编码
我有一个使用 Builder 的小 ruby 脚本。
require 'rubygems'
require 'builder'
content = <<eos
SOME TEXT, GOES TO UPPERCASE
other text
<em>italics<em>
eos
xml = Builder::XmlMarkup.new
xml.instruct! :xml, :version => '1.0'
xml.book :id => 1.0 do
xml.keyPic "keyPic1.jpg"
xml.parts do
xml.part :partId => "1", :name => "name" do
xml.chapter :title => "title", :subtitle => "subtitle" do
xml.text content
end
end
end
end
p xml
从 CLI (Cygwin) 运行时,我得到以下信息:
<?xml version="1.0" encoding="UTF-8"?>
<book id="1.0">
<keyPic>keyPic1.jpg</keyPic>
<parts>
<part partId="1" name="name">
<chapter title="title" subtitle="subtitle">
<text>
SOME TEXT, GOES TO UPPERCASE
other text
<em>italics<em>
</text>
</chapter>
</part>
</parts>
</book><inspect/>
然而,我想要的输出是:
<text>
SOME TEXT, GOES TO UPPERCASE
other text
<em>italics<em/>
</text>
我尝试使用 htmlentities gem '解码' 内容,但无济于事。
I have a small ruby script which uses Builder.
require 'rubygems'
require 'builder'
content = <<eos
SOME TEXT, GOES TO UPPERCASE
other text
<em>italics<em>
eos
xml = Builder::XmlMarkup.new
xml.instruct! :xml, :version => '1.0'
xml.book :id => 1.0 do
xml.keyPic "keyPic1.jpg"
xml.parts do
xml.part :partId => "1", :name => "name" do
xml.chapter :title => "title", :subtitle => "subtitle" do
xml.text content
end
end
end
end
p xml
When running from the CLI (Cygwin), I get the following:
<?xml version="1.0" encoding="UTF-8"?>
<book id="1.0">
<keyPic>keyPic1.jpg</keyPic>
<parts>
<part partId="1" name="name">
<chapter title="title" subtitle="subtitle">
<text>
SOME TEXT, GOES TO UPPERCASE
other text
<em>italics<em>
</text>
</chapter>
</part>
</parts>
</book><inspect/>
However, the output I would like between is:
<text>
SOME TEXT, GOES TO UPPERCASE
other text
<em>italics<em/>
</text>
I have tried using the htmlentities gem 'decoding' the content but to no avail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
<<
操作插入不加修改的文本。Use the
<<
operation to insert your text without modification.