CDATA部分真的没有必要吗? [英] Are CDATA sections really unnecessary?

查看:137
本文介绍了CDATA部分真的没有必要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是由于开发人员Michael Rys相当激进地拒绝将CDATA部分的解析包括到FOR XML PATH中而引起的,因为

我已经将HTML块存储在CDATA节点和其他需要使用特殊字符或笨拙字符的内容中.但是,我觉得没有资格挑战Rys的有争议的主张,因为我认为,从技术上讲,在我为了方便起见使用CDATA的情况下,他是正确的.

真正使我烦恼的是,当开发人员在互联网上乞求有关如何使用FOR XML PATH呈现CDATA段的建议时,受访者不断指示他们使用FOR XML EXPLICIT,而不是

如果我们真的可以在每个用例中都没有CDATA的情况下,有人会建议我猜,我们此后应该停止抱怨,并拒绝使用CDATA.但是,如果在明确定义了CDATA必不可少的情况下,Rys已经保证他会将其放入FOR XML PATH中,直到该问题的最上面的链接中.

那应该是什么? CDATA部分真的是过去的遗迹吗?还是Rys应该伸出手指允许在FOR XML PATH中进行CDATA解析?同时,在此期间,是否存在用于FOR XML PATH返回CDATA部分的任何技巧?

解决方案

CDATA部分如果您不关心其中数据的语义(即,您不需要解析它-只是简单的)就很有用字符),并且您不希望转义其中的任何XML.

根据 w3 的定义:

CDATA节可能出现在字符数据可能出现的任何地方;它们用于转义包含字符的文本块,否则这些字符将被视为标记.

来自维基百科:

XML文档的新作者经常误解CDATA节的目的,错误地认为它的目的是保护"数据,使其在处理过程中不被视为普通字符数据.一些用于处理XML文档的API确实提供了用于独立访问CDATA节的选项,但是这些选项存在于XML处理系统的正常要求之上和之外,并且仍然不会更改数据的隐含含义.字符数据是字符数据,无论它是通过CDATA节还是普通标记表示的.

CDATA节对于将XML代码作为XML文档中的文本数据编写很有用.例如,如果希望用XSL排版一本书来解释XML应用程序的使用,那么出现在书本中的XML标记将被写入CDATA部分的源文件中.但是,CDATA节不能包含字符串]]>",因此CDATA节不可能包含嵌套的CDATA节.使用CDATA节来编码包含三元组]]>"的文本的首选方法是通过在>"之前拆分每个出现的三元组来使用多个CDATA节.例如,要编码]]>",应这样写:

This question is prompted by the rather militant refusal of developer Michael Rys to include the parsing of CDATA sections into FOR XML PATH because "There is no semantic difference in the data that you store."

I have stored nuggets of HTML in CDATA nodes and other content that requires the use of special or awkward characters. However I don't feel qualified to challenge Rys's controversial assertion because, I suppose, technically he is correct in the scenarios where I've employed CDATA for convenience.

What's really baking my noodle is that, as developers take to the internet begging for advice on how to render CDATA segments using FOR XML PATH, respondents continually direct them to use FOR XML EXPLICIT instead, the XML rendering method Rys cited as being the "query from hell".

If we can really do without CDATA in every use case that anyone can suggest I guess we should stop moaning and reject CDATA usage henceforth. But if there are clearly defined cases where CDATA is essential Rys already undertook that he would bake it into FOR XML PATH going forward in the topmost link in this question.

So which is it to be? Are CDATA sections really relics of the past? Or should Rys pull his finger out and allow for CDATA parsing in FOR XML PATH? And while we're at it, in the meanwhile, are there any hacks for getting FOR XML PATH to return CDATA sections?

解决方案

CDATA sections are useful if you don't care about the semantics of the data in them (i.e. you do not need to parse it - it is simply a run of characters), and you don't wish to escape any of the XML within them.

The definition, according to w3:

CDATA sections may occur anywhere character data may occur; they are used to escape blocks of text containing characters which would otherwise be recognized as markup.

From wikipedia:

New authors of XML documents often misunderstand the purpose of a CDATA section, mistakenly believing that its purpose is to "protect" data from being treated as ordinary character data during processing. Some APIs for working with XML documents do offer options for independent access to CDATA sections, but such options exist above and beyond the normal requirements of XML processing systems, and still do not change the implicit meaning of the data. Character data is character data, regardless of whether it is expressed via a CDATA section or ordinary markup.

CDATA sections are useful for writing XML code as text data within an XML document. For example, if one wishes to typeset a book with XSL explaining the use of an XML application, the XML markup to appear in the book itself will be written in the source file in a CDATA section. However, a CDATA section cannot contain the string "]]>" and therefore it is not possible for a CDATA section to contain nested CDATA sections. The preferred approach to using CDATA sections for encoding text that contains the triad "]]>" is to use multiple CDATA sections by splitting each occurrence of the triad just before the ">". For example, to encode "]]>" one would write:

这篇关于CDATA部分真的没有必要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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