Search code examples
xmlxsltkml

Removing Space XSLT


I have found many posts which concern my problem but the solutions don't work.

I have many XML elements in my input and I want generate a KML file. For the KML description I use CDATA. In this CDATA I try to put an <img>.

So my XSL:

<xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
                    <xsl:text disable-output-escaping="yes">   
                         &lt;p&gt;The &lt;a href="http://en.wikipedia.org/wiki/Fremont_Troll">Fremont Troll&lt;/a&gt; is a large concrete sculpture &lt;br/&gt;
                            of the mythical troll who lives under the bridge.&lt;/p&gt;
                             &lt;p&gt;The troll is holding a Volkswagen bug in his hand.&lt;/p&gt;
                                &lt;img src="http://client.thomasblasig.fr/KML_images  
                    </xsl:text>
            <xsl:variable name="link_image" select="$images/circuits/circuit[@id=$id_circuit]/image[@etape=$id_etape]/lien"></xsl:variable>
            <xsl:variable name="new_link_image" select="replace($link_image, './', '/')"></xsl:variable>        
            <xsl:value-of select="translate($new_link_image, ' ', '')"></xsl:value-of>
                    <xsl:text disable-output-escaping="yes">    
                                "&gt;&lt;/img&gt;
                    </xsl:text>
              <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>

The code produces this result:

<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<Placemark>
<name>Etape n° 1 : Avenue Joliot Curie</name>
<description>
<![CDATA[
<p>The <a href="http://en.wikipedia.org/wiki/Fremont_Troll">Fremont Troll</a> is a large concrete sculpture <br/> of the mythical troll who lives under the bridge.</p> <p>The troll is holding a Volkswagen bug in his hand.</p> <img src="http://client.thomasblasig.fr/KML_images /image/02.jpg "></img>
]]>
</description>
<Point>
<coordinates>-74.006393,40.714172,0</coordinates>
</Point>
</Placemark>

As you can see, there are whitespaces between my <xsl:text> and my <xsl:valueof>. How can I remove them?


Solution

  • I am guessing the whitespace you are talking about, is the whitespaces occur in the img tag in your output:

    <img src="http://client.thomasblasig.fr/KML_images /image/02.jpg ">
    

    If this is the case, it is not the whitespace between the xsl:text and xsl:value-of that is causing the issue (as whitespace-only nodes in the XSLT are not significant), but the whitespace within the xsl:text itself

    So, this xsl:text...

    <xsl:text disable-output-escaping="yes">    
        "&gt;&lt;/img&gt;
    </xsl:text>
    

    ... really needs to be written like this:

    <xsl:text disable-output-escaping="yes">"&gt;&lt;/img&gt;</xsl:text>
    

    And similarly for the first xsl:text, it needs to end like this...

    &lt;img src="http://client.thomasblasig.fr/KML_images</xsl:text>
    

    Note that you might consider re-writing your XSLT to be like this, to give it a semblance of readability

        <description>
        <![CDATA[<p>The <a href="http://en.wikipedia.org/wiki/Fremont_Troll">Fremont Troll</a> is a large concrete sculpture <br/>
                of the mythical troll who lives under the bridge.</p>
                <p>The troll is holding a Volkswagen bug in his hand.</p>
                <img src="http://client.thomasblasig.fr/KML_images]]><xsl:value-of select="translate($new_link_image, ' ', '')" /><![CDATA["></img>]]>
        </description>
    

    (This would probably have to be combined with the cdata-section-elements attribute on the xsl:output element)

    <xsl:output method="xml" indent="yes" cdata-section-elements="description" />