Question

I'm wondering how to replace a string say AUD with image if the text matches. I have this xsl code and not sure how to use if condition since I'm new to xsl coding.

<xsl:template match="/">
    <table class="headingstable">
      <tr>
        <th class="headingstop">Title</th>
        <th class="headingstop">Country</th>
         <th class="headingstop">Date</th>

      </tr>
      <xsl:for-each select="weeklyevents/event">
    <tr>
      <td class="headingsmid"><xsl:value-of select="title"/></td>
      <td class="headingsmid"><xsl:value-of select="country"/></td>
      <td class="headingsmid"><xsl:value-of select="date"/></td>
    </tr>
    </xsl:for-each>
    </table>

</xsl:template>

The country select can be one of the four in display results: USA, AUS, UK and JP. so I want to create an IF condition " if string = UK then <img src="../uk.jpg" />

I'm not sure how to do that, but tried many times like this:

<xsl:template match="country[. = 'UK']">
  <img src="../uk.jpg" />
</xsl:template>

how do I do that correctly??

Update#1, this is my xml code:

    <?xml version="1.0" encoding="utf-8"?>
    <weeklyevents>
        <event>
            <title>AIG Services Index</title>
            <country>AUD</country>
            <date><![CDATA[05-04-2014]]></date>
        </event>
        <event>
            <title>Bank Holiday</title>
            <country>JPY</country>
            <date><![CDATA[05-05-2014]]></date>
        </event>
        <event>
            <title>MI Inflation Gauge m/m</title>
            <country>AUD</country>
            <date><![CDATA[05-05-2014]]></date>
        </event>
        <event>
            <title>ANZ Job Advertisements m/m</title>
            <country>AUD</country>
            <date><![CDATA[05-05-2014]]></date>
        </event>
        <event>
            <title>Building Approvals m/m</title>
            <country>AUD</country>
            <date><![CDATA[05-05-2014]]></date>
        </event>
        <event>
            <title>HSBC Final Manufacturing PMI</title>
            <country>CNY</country>
            <date><![CDATA[05-05-2014]]></date>
        </event>
        <event>
            <title>Bank Holiday</title>
            <country>GBP</country>
            <date><![CDATA[05-05-2014]]></date>
        </event>
        <event>
            <title>Sentix Investor Confidence</title>
            <country>EUR</country>
            <date><![CDATA[05-05-2014]]></date>
        </event>
        <event>
            <title>EU Economic Forecasts</title>
            <country>EUR</country>
            <date><![CDATA[05-05-2014]]></date>
        </event>
        <event>
            <title>PPI m/m</title>
            <country>EUR</country>
            <date><![CDATA[05-05-2014]]></date>
        </event>
        <event>
            <title>Eurogroup Meetings</title>
            <country>EUR</country>
            <date><![CDATA[05-05-2014]]></date>
        </event>
        <event>
            <title>Final Services PMI</title>
            <country>USD</country>
            <date><![CDATA[05-05-2014]]></date>
        </event>
        <event>
            <title>ISM Non-Manufacturing PMI</title>
            <country>USD</country>
            <date><![CDATA[05-05-2014]]></date>
        </event>
        <event>
            <title>Loan Officer Survey</title>
            <country>USD</country>
            <date><![CDATA[05-05-2014]]></date>
        </event>
        <event>
            <title>Bank Holiday</title>
            <country>JPY</country>
            <date><![CDATA[05-06-2014]]></date>
        </event>
        <event>
            <title>Trade Balance</title>
            <country>AUD</country>
            <date><![CDATA[05-06-2014]]></date>
        </event>
        <event>
            <title>Cash Rate</title>
            <country>AUD</country>
            <date><![CDATA[05-06-2014]]></date>
        </event>
        <event>
            <title>RBA Rate Statement</title>
            <country>AUD</country>
            <date><![CDATA[05-06-2014]]></date>
        </event>
        <event>
            <title>Spanish Unemployment Change</title>
            <country>EUR</country>
            <date><![CDATA[05-06-2014]]></date>
        </event>
        <event>
            <title>Spanish Services PMI</title>
            <country>EUR</country>
            <date><![CDATA[05-06-2014]]></date>
        </event>
        <event>
            <title>Italian Services PMI</title>
            <country>EUR</country>
            <date><![CDATA[05-06-2014]]></date>
        </event>
        <event>
            <title>Final Services PMI</title>
            <country>EUR</country>
            <date><![CDATA[05-06-2014]]></date>
        </event>
        <event>
            <title>Services PMI</title>
            <country>GBP</country>
            <date><![CDATA[05-06-2014]]></date>
        </event>
</weeklyevents>
Was it helpful?

Solution

You can change

<xsl:value-of select="country"/>

by

        <xsl:choose>
          <xsl:when test="country = 'USA'">
            <img src="../usa.jpg" />
          </xsl:when>
          <xsl:when test="country = 'UK'">
            <img src="../uk.jpg" />
          </xsl:when>
          <xsl:otherwise>
            ...
          </xsl:otherwise>
        </xsl:choose>               

OTHER TIPS

If you want the template that includes the image tag to be processed, you should replace the <xsl:value-of select="country"/> with xsl:apply-templates select="country">. With that you will call the templates you defined for each country.

<xsl:template match="/">
    <table class="headingstable">
        ...
        <xsl:for-each select="weeklyevents/event">
            <tr>
                <td class="headingsmid"><xsl:value-of select="title"/></td>
                <td class="headingsmid"><xsl:apply-templates select="country"/></td>
                <td class="headingsmid"><xsl:value-of select="date"/></td>
            </tr>
        </xsl:for-each>
    </table>
</xsl:template>

So if you have a template for each country, as you proposed, if there is <country>UK</country> in the source document, it will match one of your country templates:

<xsl:template match="country[. = 'UK']">
  <img src="../uk.jpg" />
</xsl:template>

and the generated <td> will be:

<td class="headingsmid">
     <img src="../uk.jpg"/>
</td>

In case the source contains an "unsupported" country (one you don't have a template for), it will simply print out the text:

<td class="headingsmid">AUS</td>

If you can derive your file name from the country code, then you can make your stylesheet even simpler.

If you are using XSLT 2.0 replace all your country templates for:

<xsl:template match="country">
    <img src="../{lower-case(.)}.jpg" />
</xsl:template>

If you are limited to XSLT 1.0 (if processing is done in a browser, for example), you won't have lower-case() but can use translate() like this:

<xsl:template match="country">
    <img src="../{translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')}.jpg" />
</xsl:template>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top