Populating column dynamically from key value pair

319 views Asked by At

I am working with an xslt file for styling. I am showing some content in tabular format. I need to dynamically populate one column from pre defined key value pair. Please look at below example

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
  <h2>Sample Transformation</h2>
    <table border="1">
    <xyz>msgprop</xyz>
      <tr bgcolor="#9acd32">
        <th>Id</th>
        <th>Name</th>
        <th>City</th>
      </tr>
      <xsl:for-each select="en:MyEvent">
      <tr>
        <td><xsl:value-of select="en:id"/></td>
        <td><xsl:value-of select="en:name"/></td>
        <td><xsl:value-of select="en:country"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

The problem is that i am receiving country name in place of city and i want to populate the city column with correct city and for that i need to map country and its city. Is there any way to predefined country and its city in key value pair and when received the country name, it will be replaced by city name(if not present it will display country name). Is there any function to do it?. It will be helpfull if you provide a snippet.

Edit: Here is the xml

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <cd>
    <id>Empire Burlesque</id>
    <Name>Bob Dylan</Name>
    <country>USA</country>
  </cd>
  <cd>
    <id>Hide your heart</id>
    <name>Bonnie Tyler</name>
    <country>UK</country>
  </cd>
  <cd>
    <id>Greatest Hits</id>
    <name>Dolly Parton</name>
    <country>USA</country>
  </cd>
  <cd>
    <id>Still got the blues</id>
    <name>Gary Moore</name>
    <country>UK</country>
  </cd>
</catalog>

I need a mapping such that whenever country name is UK it will be replaced with its capital London same with other countries. Is there somew way that i define UK as a key and London as a value so that whenever i received key as UK it will be replaced with its value. Please help.

1

There are 1 answers

3
michael.hor257k On BEST ANSWER

Consider the following example:

XML

<events>
    <event>
        <id>001</id>
        <name>Alpha</name>
        <city>Paris</city>
    </event>
    <event>
        <id>002</id>
        <name>Bravo</name>
        <city>UK</city>
    </event>
    <event>
        <id>003</id>
        <name>Charlie</name>
        <city>Berlin</city>
    </event>
    <event>
        <id>004</id>
        <name>Delta</name>
        <city>USA</city>
    </event>
    <event>
        <id>005</id>
        <name>Echo</name>
        <city>Los Angeles</city>
    </event>
</events>

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/events">
    <html>
        <body>
            <h2>Sample Transformation</h2>
            <table border="1">
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>City</th>
                </tr>
                <xsl:for-each select="event">
                    <tr>
                        <td>
                            <xsl:value-of select="id"/>
                        </td>
                        <td>
                            <xsl:value-of select="name"/>
                        </td>
                        <td>
                            <xsl:choose>
                                <xsl:when test="city = 'UK'">London</xsl:when>
                                <xsl:when test="city = 'USA'">Washington, D.C.</xsl:when>
                                <!-- add more key/value pairs here -->
                                <xsl:otherwise>
                                    <xsl:value-of select="city"/>
                                </xsl:otherwise>
                            </xsl:choose>
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

Result (rendered):

enter image description here