如何在 XML 文档中包含与号 (&) 字符? [英] How can I include an ampersand (&) character in an XML document?

查看:35
本文介绍了如何在 XML 文档中包含与号 (&) 字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在保持 XML 有效的同时在 XML 标记的属性中放置 & 符号?

How can I put an & symbol in an attribute of an XML tag while keeping the XML valid?

<?xml version="1.0" ?> 
<a text="b&c"/>

当我使用 W3School 的验证器时,出现错误:

When I use W3School's validator, I get the error:

EntityRef: 期待 ';'

EntityRef: expecting ';'

推荐答案

使用字符引用来表示:&amp;

请参阅规范:

与符号 (&) 和左尖括号 (<) 不得以其文字形式出现,除非用作标记定界符,或者在注释、处理指令或 CDATA 部分中.如果其他地方需要它们,它们必须使用数字字符引用或字符串&amp;"和 "&lt;" 分别.右尖括号 (>) 可以使用字符串 "<代码>>",并且为了兼容性,必须使用 "<代码>>" 或出现在字符串中时的字符引用 "<代码>]]>" 在内容中,当该字符串未标记 CDATA 部分的结尾时.

The ampersand character (&) and the left angle bracket (<) MUST NOT appear in their literal form, except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. If they are needed elsewhere, they MUST be escaped using either numeric character references or the strings " &amp; " and " &lt; " respectively. The right angle bracket (>) may be represented using the string " &gt; ", and MUST, for compatibility, be escaped using either " &gt; " or a character reference when it appears in the string " ]]> " in content, when that string is not marking the end of a CDATA section.

这篇关于如何在 XML 文档中包含与号 (&amp;) 字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆