在XML文档中必须转义哪些字符,或者在哪里可以找到这样的列表?


当前回答

对于标签和属性,转义字符是不同的。

标签:

 < &lt;
 > &gt; (only for compatibility, read below)
 & &amp;

属性:

" &quot;
' &apos;

从字符数据和标记:

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. To allow attribute values to contain both single and double quotes, the apostrophe or single-quote character (') may be represented as " &apos; ", and the double-quote character (") as " &quot; ".

其他回答

对于标签和属性,转义字符是不同的。

标签:

 < &lt;
 > &gt; (only for compatibility, read below)
 & &amp;

属性:

" &quot;
' &apos;

从字符数据和标记:

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. To allow attribute values to contain both single and double quotes, the apostrophe or single-quote character (') may be represented as " &apos; ", and the double-quote character (") as " &quot; ".

也许这将有所帮助:

XML和HTML字符实体引用列表:

在SGML、HTML和XML文档中 被称为字符的逻辑结构 数据和属性值由 字符序列,其中每个 性格可以直接表现出来 (代表自己),或者可以 由一系列字符表示的 称为字符引用,其中 有两种类型:数字类型 字符引用和一个字符 实体引用。本文列出了 字符实体引用它 在HTML和XML文档中有效。

那篇文章列出了以下五个预定义的XML实体:

quot  "
amp   &
apos  '
lt    <
gt    >

对一个老问题的新的、简化的回答……

简化XML转义(有优先级,100%完成)

Always (90% important to remember) Escape < as &lt; unless < is starting a <tag/> or other markup. Escape & as &amp; unless & is starting an &entity;. Attribute Values (9% important to remember) attr=" 'Single quotes' are ok within double quotes." attr=' "Double quotes" are ok within single quotes.' Escape " as &quot; and ' as &apos; otherwise. Comments, CDATA, and Processing Instructions (0.9% important to remember) <!-- Within comments --> nothing has to be escaped but no -- strings are allowed. <![CDATA[ Within CDATA ]]> nothing has to be escaped, but no ]]> strings are allowed. <?PITarget Within PIs ?> nothing has to be escaped, but no ?> strings are allowed. Esoterica (0.1% important to remember) Escape control codes in XML 1.1 via Base64 or Numeric Character References. Escape ]]> as ]]&gt; unless ]]> is ending a CDATA section. (This rule applies to character data in general – even outside a CDATA section.)

这取决于上下文。对于内容,它是<和&,和]]>(尽管是一个由三个字符组成的字符串而不是一个字符)。

对于属性值,它是<、&、"和'。

对于CDATA,为[]>。

摘自:XML,转义

有五个预定义的实体:

&lt; represents "<"
&gt; represents ">"
&amp; represents "&"
&apos; represents '
&quot; represents "

所有允许的Unicode字符都可以用数字字符引用表示。例如:

&#20013;

大多数控制字符和其他Unicode范围被明确排除,这意味着(我认为)它们不能出现转义或直接:

XML中的有效字符