什么时候需要在XML中转义字符? [英] When is it required to escape characters in XML?

查看:188
本文介绍了什么时候需要在XML中转义字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

何时应该替换 > &安培; 在XML中的字符,如& lt 等。

When should we replace < > & " ' in XML to characters like &lt etc.

我的理解是这只是为了确保如果XML的内容部分具有>< ,解析器将不会被视为标签的开始或结束。

My understanding is that it's just to make sure that if the content part of XML has > < the parser will not treat is start or end of a tag.

另外,如果我有一个XML,如:

Also, if I have a XML like:

<hello>mor>ning<hello>

应该将其替换为:


  • & lthello& gtmor& gtning& lthello& gt

  • & lthello& gtmor> ning& lthello& gt

  • < hello> mor& gtning< hello>

  • &lthello&gtmor&gtning&lthello&gt
  • &lthello&gtmor>ning&lthello&gt
  • <hello>mor&gtning<hello>

我不明白为什么需要更换,当需要什么,什么是标签或文本被替换?

I don't understand why replacing is needed. When exactly is it required and what exactly (tags or text) should be replaced?

推荐答案

< / code>,& code>在XML中都有特殊含义(例如实体开始或属性值分隔符)。

<, >, &, " and ' all have special meanings in XML (such as "start of entity" or "attribute value delimiter").

为了使这些字符显示为数据(而不是其特殊含义),可以是由实体代表(& lt; for 等等)。

In order to have those characters appear as data (instead of for their special meaning) they can be represented by entities (&lt; for < and so on).

有时这些特殊含义是上下文敏感的(例如并不意味着标签之外的属性分隔符),并且有一些地方可以显示为原始数据,而是担心这些异常,只要总是将其代表为实体,那么最简单的就是要避免它们的特殊那么唯一的getcha是明确的CDATA部分,特殊含义不成立(而& 不会启动一个实体)。

Sometimes those special meanings are context sensitive (e.g. " doesn't mean "attribute delimiter" outside of a tag) and there are places where they can appear raw as data. Rather then worry about those exceptions, it is simplest to just always represent them as entities if you want to avoid their special meaning. Then the only gotcha is explicit CDATA sections where the special meaning doesn't hold (and & won't start an entity).


应该将其替换为

should this be replaced to either

不应该被表示作为任何一个,实体必须以分号终止。

It shouldn't be represented as any of those. Entities must be terminated with a semi-colon.

你应该如何表示它取决于你的数据示例哪个是哪个标记。没有说,例如,如果< hello> 应该是一个hello元素的数据或开始标签。

How you should represent it depends on which bit of your example of data and which is markup. You haven't said, for example, if <hello> is supposed to be data or the start tag for a hello element.

这篇关于什么时候需要在XML中转义字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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