Add CDATA to an xml file

让人想犯罪 __ 提交于 2019-11-27 09:28:13

You either need to do this:

<xsl:template match="teaserText_fr">
  <xsl:copy>
    <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
    <xsl:copy-of select="*"/>    
    <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
  </xsl:copy>
</xsl:template>

Or this:

<xsl:template match="teaserText_fr">
  <teaserText_fr>
    <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
    <xsl:copy-of select="*"/>    
    <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
  </teaserText_fr>
</xsl:template>

(I recommend the first approach)

and you should be all set.

To give the same treatment to any element whose name starts with "teaserText_":

<xsl:template match="*[starts-with(local-name(), 'teaserText_')]">
  <xsl:copy>
    <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
    <xsl:copy-of select="*"/>    
    <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
  </xsl:copy>
</xsl:template>
Shoaib Khan

A cleaner approach would be to make use of cdata-section-elements

Delcare teaserText_fr in cdata-section-elements as below

<xsl:output method="xml" indent="yes" version="1.0" encoding="UTF-16" 
standalone="yes" cdata-section-elements="teaserText_fr" />

Then format the XSLT as below. (Do note that you must include CDATA as a wrapper around the element)

<xsl:template match="/">
    <teaserText_fr>
        <![CDATA[
            <div xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.coremedia.com/2003/richtext-1.0"><p>2012 ist für viele Länder ein   wichtiges Wahljahr. Die Reihe fühlt der weltweiten Demokratie auf den Zahn. </p>
            </div>
        ]]>
    </teaserText_fr>
</xsl:template>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!