在XML文档中需要转义什么字符? [英] What characters do I need to escape in XML documents?

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

问题描述

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

解决方案

/ h1>

只有五个:

 & quot; 
'&';
<& lt;
>& gt;
& amp; amp;
pre>

转义字符取决于特殊字符的使用位置。



示例可以在 W3C标记验证服务



文本< h2>

安全的方法是转义文本中的所有五个字符,但是,三个字符'> 不必在文本中转义:

 <?xml version =1.0?> 
< valid>'>< / valid>



属性



安全的方法是转义属性中的所有五个字符,但是> 字符不需要在属性中转义:

 <?xml version =1.0?> 
< valid attribute =& />

'如果引号

  ?xml version =1.0?> 
< valid attribute ='/>

同样,如果引号是',则在属性中转义:

 <?xml version =1.0?> 
< valid attribute =''/>



注释



strong>不得在注释中转义:

 <?xml version =1.0?& 
< valid>
<! - '<>& - >
< / valid>



CDATA



不得 en.wikipedia.org/wiki/CDATArel =nofollow noreferrer> CDATA 部分:

 < ?xml version =1.0?> 
< valid>
<![CDATA ['<
< / valid>



处理指示



不得在XML处理说明中转义:

 <?xml version =1.0? > 
<?process<'&>?>
< valid />

$ b b

XML与HTML



HTML有自己的一组转义代码,其中包含更多字符。


What characters must be escaped in XML documents, or where could I find such a list?

解决方案

XML escape characters

There are only five:

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

Escaping characters depends on where the special character is used.

The examples can be validated at W3C Markup Validation Service.

Text

The safe way is to escape all five characters in text, however, the three characters ", ' and > needn't be escaped in text:

<?xml version="1.0"?>
<valid>"'></valid>

Attributes

The safe way is to escape all five characters in attributes, however, the > character needn't be escaped in attributes:

<?xml version="1.0"?>
<valid attribute=">"/>

The ' character needn't be escaped in attributes if the quotes are ":

<?xml version="1.0"?>
<valid attribute="'"/>

Likewise, the " needn't be escaped in attributes if the quotes are ':

<?xml version="1.0"?>
<valid attribute='"'/>

Comments

All 5 special characters must not be escaped in comments:

<?xml version="1.0"?>
<valid>
<!-- "'<>& -->
</valid>

CDATA

All 5 special characters must not be escaped in CDATA sections:

<?xml version="1.0"?>
<valid>
<![CDATA["'<>&]]>
</valid>

Processing instructions

All 5 special characters must not be escaped in XML processing instructions:

<?xml version="1.0"?>
<?process <"'&> ?>
<valid/>

XML vs. HTML

HTML has its own set of escape codes which cover a lot more characters.

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

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