Add CDATA to an xml file
Asked Answered
S

2

12

I would like to add some CDATA tags around some xml tags

XML source is (it's only a small part of my file)

<teaserText_fr>
<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>

What I would like is

<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>

My xslt is

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output 
  method="html"
  encoding="UTF-8"
  omit-xml-declaration="yes"
  doctype-public="-//W3C//DTD HTML 4.01//EN"
  doctype-system="http://www.w3.org/TR/html4/strict.dtd"  
  indent="yes" />  

<xsl:template match="node()|@*">
  <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="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>
</xsl:template>

</xsl:stylesheet>

What I get is

</teaserText_de><![CDATA[<div xmlns="http://www.coremedia.com/2003/richtext-1.0" xmls:xlink="http://www.w3.org/1999/xlink"><p>à partir du 10 janvier, ARTE diffuse "I love democracy", une série documentaire qui, en cette grand année électorale, prend le pouls démocratique de la planète.</p></div>]]><addTeaserText_de>

I lost my teaserText_fr tags, I don't understand why

If possible, I would like to do so for some extra tags (with regexp like [add|]TeaserText_[fr|de] but I can't get it work ... "

I did some tests all day long but I wasn't succesfull.

Best regards, Guillaume

Simulcast answered 20/3, 2013 at 20:40 Comment(0)
F
22

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>
Foolery answered 20/3, 2013 at 20:51 Comment(2)
Thanks JLRishe, it works with your first approach (even if I don't understand all right now). How could I add regexp to have the same result for teaserTest_de by example.Simulcast
All I did was add an xsl:copy which does a shallow copy the context node (in this case, it's a teaserText_fr). Essentially, when the current node is an element, it puts tags for the current element around what's inside. I'm not sure you need a regex to also handle "teaserText_de"; how about the template I've added at the end of my answer?Foolery
P
4

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>
Putumayo answered 3/11, 2016 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.