Why should you use XML CDATA blocks?

╄→尐↘猪︶ㄣ 提交于 2019-12-01 18:53:18

CDATA is just the standard way of keeping the original text as is, meaning that whatever application processes the XML shouldn't need to take any explicit action to unescape.

You get that typically with JavaScript embedded in XHTML, when you use reserved symbols:

<script type="text/javascript">
//<![CDATA[
    var test = "<This is a string with reserved characters>";

    if (1 > 0) {
        alert(test);
    }
//]]>
</script>

If you had if (1 &gt; 0) instead, it would have to unescape explicitly (which it doesn't). It's also much more readable like this.

It's first and most of all readability feature. XML, and SGML before it were originally meant to be human readable - believe it or not :-))

Second, for a good parser it's a perf feature. The ]]> ending is guaranteed to be the actual block ending but apart from that it's a blob.

By the book parser should also keep the info/attrib on a node representation to track that it has explicit CDATA and never ever touch a single char in it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!