如何在XML文档中包含&字符?

时间:2022-09-13 09:05:36

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

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

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

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

当我使用W3School的验证器时,我得到了错误:

EntityRef: expecting ';'

EntityRef:期待“;”

2 个解决方案

#1


43  

Use a character reference to represent it: &amp;

使用字符引用来表示它:&

See the specification:

看到规格:

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.

符号和字符(&)和左角括号(<)不能以文字形式出现,除非用作标记分隔符,或在注释、处理指令或CDATA节中使用。如果在其他地方需要它们,则必须使用数字字符引用或字符串“&”和“& lt;分别”。直角支架(>)可以用“>”字符串表示;为了兼容性,必须使用“>“或字符串中出现的字符引用”]>“在内容中,当该字符串没有标记CDATA部分的末尾时。

#2


0  

You can use these escape sequences :

您可以使用以下转义序列:

< (less-than)                   -   &#60; or &lt;

> (greater-than)                -   &#62; or  &gt;

& (ampersand)                   -   &#38;

' (apostrophe or single quote)  -   &#39;

" (double-quote)                -   &#34;

#1


43  

Use a character reference to represent it: &amp;

使用字符引用来表示它:&

See the specification:

看到规格:

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.

符号和字符(&)和左角括号(<)不能以文字形式出现,除非用作标记分隔符,或在注释、处理指令或CDATA节中使用。如果在其他地方需要它们,则必须使用数字字符引用或字符串“&”和“& lt;分别”。直角支架(>)可以用“>”字符串表示;为了兼容性,必须使用“>“或字符串中出现的字符引用”]>“在内容中,当该字符串没有标记CDATA部分的末尾时。

#2


0  

You can use these escape sequences :

您可以使用以下转义序列:

< (less-than)                   -   &#60; or &lt;

> (greater-than)                -   &#62; or  &gt;

& (ampersand)                   -   &#38;

' (apostrophe or single quote)  -   &#39;

" (double-quote)                -   &#34;