问题描述
我在处理xml中的\ line-feed和\ carriage-return字符时遇到了一个问题.我知道,根据 http://www.w3.org /TR/REC-xml/#sec-line-ends ,需要xml处理器将任何"\ n \ r"或单独的"\ r"序列替换为"\ n".
规范指出,这必须是处理任何外部解析实体"的行为,这是否也适用于元素内部的CDATA节?谢谢你,
I've stumbled in a problem handling the \line-feed and \carriage-return characters in xml.I know that, according to http://www.w3.org/TR/REC-xml/#sec-line-ends, xml processors are required to replace any "\n\r" or lone "\r" sequences with "\n".
The specification states that this has to be the behaviour for handling any "external parsed entity", does this apply to CDATA sections inside of an element as well?
thank you,
米歇尔
我敢肯定,例如msxml库会将每个\ n \ r或单独的" \ r序列转换为" \ n,无论它们是否位于cdata节中.
I'm sure that msxml library for example converts every \n\r" or lone "\r" sequences to "\n", regardless of their being in a cdata section or not.
推荐答案
我将引用您链接到的部分的句子(强调我的意思):
I'll quote a sentence from the section you link to (emphasis mine):
因为XML处理器在解析之前会执行此操作,所以它尚不知道文档的哪些部分是CDATA节.因此,无论字符是否在CDATA节中,它都会进行替换.
Because the XML processor does this before parsing, it doesn't know yet which parts of the document are CDATA sections. Therefore, it will do the replacement regardless of the characters being in a CDATA section or not.
要可靠地保留这些字符,必须将它们作为
和

实体写入XML文档.
To reliably preserve these characters, they have to be written to the XML document as 
and 

entities.
这篇关于XML规范是否规定解析器需要始终将\ n \ r转换为\ n,即使\ n \ r出现在CDATA节中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!