theta
theta

Reputation: 25591

XSLT template match and XPath

I have XML file:

...
    <Chart id="c01">
      <expires>2012-07-19 12:20:00</expires>
      <data><![CDATA[...]]></data>
    </Chart>
...

where CDATA is B64 encoded PNG image, and whanted to transform XML to HTML to view the image.

For that purpose I set XSL to output text and dump CDATA:

<xsl:output method="text"/>

<xsl:template match="//data/text()">
  <xsl:value-of select="."/>
</xsl:template>

In the same template I wrap basic HTML structure, which I excluded here for clearance.

The problem is this: If I use XPath on XML file //data/text() I get just CDATA text, but with above XSL I get also "2012-07-19 12:20:00" from <expires> XML element, in the output.

I guess it's some basic thing I'm missing, but why is date also outputed and how can I avoid it in output?

Upvotes: 1

Views: 1512

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

Probably the shortest way to achieve this is:

 <xsl:template match="*[not(self::data)]/text()"/>

The complete transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="*[not(self::data)]/text()"/>
</xsl:stylesheet>

When this transformation is applied on the XML document:

<t>
    <Chart id="c01">
        <expires>2012-07-19 12:20:00</expires>
        <data><![CDATA[ImageBlob]]></data>
    </Chart>
</t>

the wanted, correct result is produced:

ImageBlob

Upvotes: 1

Related Questions