如何转义 XML 中的 & 符号,以便它们在 HTML 中呈现为实体?
我有一些 XML 文本希望在 HTML 页面中呈现。该文本包含一个 & 符号,我想在其实体表示中呈现它:&
。
如何在源 XML 中转义这个 & 符号?我尝试了 &
,但这被解码为实际的&字符(&
),即 在 HTML 中无效。
所以我想以这样的方式转义它,使其在使用 XML 输出的网页中呈现为 &
。
I have some XML text that I wish to render in an HTML page. This text contains an ampersand, which I want to render in its entity representation: &
.
How do I escape this ampersand in the source XML? I tried &
, but this is decoded as the actual ampersand character (&
), which is invalid in HTML.
So I want to escape it in such a way that it will be rendered as &
in the web page that uses the XML output.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
当您的 XML 包含
&
时,将生成文本&
。当您在 HTML 中使用它时,它将呈现为
&
。When your XML contains
&
, this will result in the text&
.When you use that in HTML, that will be rendered as
&
.根据 XML 1.0 规范的§2.4,您应该能够使用
&
。你确定这不是一个不同的问题吗? XML 明确地将其定义为转义 & 符号的方式。
As per §2.4 of the XML 1.0 spec, you should be able to use
&
.Are you sure it isn't a different issue? XML explicitly defines this as the way to escape ampersands.
&
字符本身就是 XML 中的转义字符,因此解决方案是将其与&
的 Unicode 十进制等效字符连接起来,从而确保不存在 XML 解析错误。即,将字符&
替换为&
。The
&
character is itself an escape character in XML so the solution is to concatenate it and a Unicode decimal equivalent for&
thus ensuring that there are no XML parsing errors. That is, replace the character&
with&
.使用
CDATA
标签:Use
CDATA
tags:&
应该可以正常工作。 Wikipedia 有一个 XML 格式的预定义实体列表。&
should work just fine. Wikipedia has a list of predefined entities in XML.就我而言,我必须将其更改为
%26
。我需要在 URL 中转义
&
。所以&
对我来说不起作用。urlencode 函数将
&
更改为%26
。这样,XML 和浏览器 URL 机制都不会抱怨 URL。In my case I had to change it to
%26
.I needed to escape
&
in a URL. So&
did not work out for me.The urlencode function changes
&
to%26
. This way neither XML nor the browser URL mechanism complained about the URL.&
是在 XML 文档的大多数部分中表示 & 符号的方式。如果您希望在 HTML 中显示 XML,则需要首先创建正确编码的 XML(这涉及将
&
更改为&
),然后 /em> 使用它来创建正确编码的 HTML(这涉及再次将&
更改为&
)。结果是:有关 XML 编码的更全面的解释,请参阅:
我需要在 XML 文档中转义哪些字符?
&
is the way to represent an ampersand in most sections of an XML document.If you want to have XML displayed within HTML, you need to first create properly encoded XML (which involves changing
&
to&
) and then use that to create properly encoded HTML (which involves again changing&
to&
). That results in:For a more thorough explanation of XML encoding, see:
What characters do I need to escape in XML documents?
我已经尝试过,但没有成功。基于 维姆·十·布林克的回答 我尝试过并且成功了。
我的一位开发同事建议我使用 &无论渲染多少次,它都有效。
I have tried &, but it didn't work. Based on Wim ten Brink's answer I tried & and it worked.
One of my fellow developers suggested me to use & and that worked regardless of how many times it may be rendered.
&
就可以了。<xsl:text disable-output-escaping="yes">& </xsl:text>
will do the trick.考虑一下您的 XML 是否如下所示。
您不能直接使用
<>
,因为它会引发错误。在这种情况下,您可以使用<>
来代替它。14.1 如何在 XML 中使用特殊字符< /a> 包含所有代码。
Consider if your XML looks like below.
You cannot use the
<>
directly as it throws an error. In that case, you can use<>
in replacement of that.14.1 How to use special characters in XML has all the codes.