在XML文档中必须转义哪些字符,或者在哪里可以找到这样的列表?
当前回答
对一个老问题的新的、简化的回答……
简化XML转义(有优先级,100%完成)
Always (90% important to remember) Escape < as < unless < is starting a <tag/> or other markup. Escape & as & 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 " and ' as ' 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 ]]> unless ]]> is ending a CDATA section. (This rule applies to character data in general – even outside a CDATA section.)
其他回答
也许这将有所帮助:
XML和HTML字符实体引用列表:
在SGML、HTML和XML文档中 被称为字符的逻辑结构 数据和属性值由 字符序列,其中每个 性格可以直接表现出来 (代表自己),或者可以 由一系列字符表示的 称为字符引用,其中 有两种类型:数字类型 字符引用和一个字符 实体引用。本文列出了 字符实体引用它 在HTML和XML文档中有效。
那篇文章列出了以下五个预定义的XML实体:
quot "
amp &
apos '
lt <
gt >
除了常见的五个字符[<,>,&,"和']之外,我还将转义垂直制表符(0x0B)。它是有效的UTF-8,但不是有效的XML 1.0,甚至许多库(包括高度可移植的(ANSI C)库libxml2)都会遗漏它,并无声地输出无效的XML。
对一个老问题的新的、简化的回答……
简化XML转义(有优先级,100%完成)
Always (90% important to remember) Escape < as < unless < is starting a <tag/> or other markup. Escape & as & 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 " and ' as ' 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 ]]> unless ]]> is ending a CDATA section. (This rule applies to character data in general – even outside a CDATA section.)
摘自:XML,转义
有五个预定义的实体:
< represents "<"
> represents ">"
& represents "&"
' represents '
" represents "
所有允许的Unicode字符都可以用数字字符引用表示。例如:
中
大多数控制字符和其他Unicode范围被明确排除,这意味着(我认为)它们不能出现转义或直接:
XML中的有效字符
根据万维网联盟(w3C)的规范,有5个字符不能以文字形式出现在XML文档中,除非用作标记分隔符或在注释、处理指令或CDATA部分中使用。在所有其他情况下,这些字符必须使用对应的实体或根据下表的数字引用替换:
Original CharacterXML entity replacementXML numeric replacement < < < > > > " " " & & & ' ' '
注意,前面提到的实体也可以在HTML中使用,除了',它是在XHTML 1.0中引入的,在HTML 4中没有声明。因此,为了确保向后兼容性,XHTML规范建议使用'代替。
推荐文章
- HtmlSpecialChars在JavaScript中等价于什么?
- 如何将XML转换成PHP数组?
- XMLHttpRequest Origin null不允许Access-Control-Allow-Origin for file:/// to file:///(无服务器)
- 如何在JavaScript中转义单引号(')?
- 如何在javadoc中转义@字符?
- LINQ读取XML
- 如何设置RecyclerView应用程序:layoutManager=""从XML?
- 如何转义JSON字符串包含换行字符使用JavaScript?
- JavaScript中变量字符串的XML解析
- 具有多个条件的XPath
- 格式化XML字符串以打印友好的XML字符串
- malformedurlexception:没有协议
- 选择包含“foo”的属性的正确XPath是什么?
- 字符串资源新行/n不可能?
- Android: ScrollView vs NestedScrollView