【问题标题】:How can I include an ampersand (&) character in an XML document?如何在 XML 文档中包含 & 字符?
【发布时间】:2013-01-08 04:14:48
【问题描述】:

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

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

当我使用W3School's validator 时,我得到了错误:

EntityRef:期待';'

【问题讨论】:

标签: xml validation


【解决方案1】:
Re: XML and ampersand in links - not parsing
You should escape & with 

> Blockquote &amp; in an XML or HTML document.ocument.

【讨论】:

    【解决方案2】:

    您可以使用这些转义序列:

    < (less-than)                   -   &#60; or &lt;
    
    > (greater-than)                -   &#62; or  &gt;
    
    & (ampersand)                   -   &#38;
    
    ' (apostrophe or single quote)  -   &#39;
    
    " (double-quote)                -   &#34;
    

    【讨论】:

    【解决方案3】:

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

    specification

    & 符号 (&) 和左尖括号 (&amp; “ 和 ” &amp;lt; " 分别。右尖括号 (>) 可以使用字符串 " &amp;gt; ",并且为了兼容性,必须使用 " &amp;gt; " 或出现在字符串中的字符引用 " ]]&gt; " 在内容中,当该字符串未标记 CDATA 部分的结尾时。

    【讨论】:

    • 我比其他问题的其他答案更喜欢这个答案,因为它引用了规范,我能够转义所有潜在的字符,而不仅仅是 OP 的一个字符
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多