如何转义 XML 中的 & 符号,以便它们在 HTML 中呈现为实体?

发布于 2024-08-02 18:57:55 字数 341 浏览 5 评论 0原文

我有一些 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 技术交流群。

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

发布评论

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

评论(10

森罗 2024-08-09 18:57:55

当您的 XML 包含 & 时,将生成文本 &

当您在 HTML 中使用它时,它将呈现为 &

When your XML contains &, this will result in the text &.

When you use that in HTML, that will be rendered as &.

柠檬色的秋千 2024-08-09 18:57:55

根据 XML 1.0 规范的§2.4,您应该能够使用&

我尝试过&但这是不允许的。

你确定这不是一个不同的问题吗? XML 明确地将其定义为转义 & 符号的方式。

As per §2.4 of the XML 1.0 spec, you should be able to use &.

I tried & but this isn't allowed.

Are you sure it isn't a different issue? XML explicitly defines this as the way to escape ampersands.

花开半夏魅人心 2024-08-09 18:57:55

& 字符本身就是 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 &.

傲鸠 2024-08-09 18:57:55

使用 CDATA 标签:

 <![CDATA[
   This is some text with ampersands & other funny characters. >>
 ]]>

Use CDATA tags:

 <![CDATA[
   This is some text with ampersands & other funny characters. >>
 ]]>
心舞飞扬 2024-08-09 18:57:55

就我而言,我必须将其更改为 %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.

无语# 2024-08-09 18:57:55

& 是在 XML 文档的大多数部分中表示 & 符号的方式。

如果您希望在 HTML 中显示 XML,则需要首先创建正确编码的 XML(这涉及将 & 更改为 &),然后 /em> 使用它来创建正确编码的 HTML(这涉及再次将 & 更改为 &)。结果是:

&amp;

有关 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:

&amp;

For a more thorough explanation of XML encoding, see:

What characters do I need to escape in XML documents?

伏妖词 2024-08-09 18:57:55

我已经尝试过,但没有成功。基于 维姆·十·布林克的回答 我尝试过并且成功了。

我的一位开发同事建议我使用 &无论渲染多少次,它都有效。

I have tried &, but it didn't work. Based on Wim ten Brink's answer I tried &amp and it worked.

One of my fellow developers suggested me to use & and that worked regardless of how many times it may be rendered.

街道布景 2024-08-09 18:57:55

就可以了。

<xsl:text disable-output-escaping="yes">& </xsl:text> will do the trick.

意中人 2024-08-09 18:57:55

考虑一下您的 XML 是否如下所示。

<Employees Id="1" Name="ABC">
  <Query>
    SELECT * FROM EMP WHERE ID=1 AND RES<>'GCF'
  <Query>
</Employees>

您不能直接使用<>,因为它会引发错误。在这种情况下,您可以使用 <> 来代替它。

<Employees Id="1" Name="ABC">
  <Query>
    SELECT * FROM EMP WHERE ID=1 AND RES <> 'GCF'
  <Query>
</Employees>

14.1 如何在 XML 中使用特殊字符< /a> 包含所有代码。

Consider if your XML looks like below.

<Employees Id="1" Name="ABC">
  <Query>
    SELECT * FROM EMP WHERE ID=1 AND RES<>'GCF'
  <Query>
</Employees>

You cannot use the <> directly as it throws an error. In that case, you can use <> in replacement of that.

<Employees Id="1" Name="ABC">
  <Query>
    SELECT * FROM EMP WHERE ID=1 AND RES <> 'GCF'
  <Query>
</Employees>

14.1 How to use special characters in XML has all the codes.

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