I’m having trouble with writing a simple XSLT transform. Here is the XML data:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader">
<lst name="params">
</lst>
</lst>
<result name="response" numFound="2" start="0">
<doc>
<str name="Race">Elf</str>
<int name="TraderKey">128</int>
<str name="TraderName">TraderLato</str>
<int name="CharacterName">Maleysh</int>
</doc>
<doc>
<str name="Race">Human</str>
<int name="TraderKey">62</int>
<str name="TraderName">TraderSam</str>
<int name="Comments">Farl</int>
</doc>
</result>
</response>
I can’t modify the format of the XML coming in, and there will be lots and lots of nodes. I need to be able to write an XSLT 1.0 transform that will copy all the original XML but replace TraderName for certain TraderKey values. On any node that contains a TraderKey of 128, change the TraderName to “Trader Lato Carum”. Any that contains a TraderKey of 62, change the TraderName to “Trader Samson Vero”. I’ve never written an XSLT before, and have limited experience with XML, so this is my attempt to write an XSLT transform:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="response/result/doc/int[@name='TraderKey'][. = '128']>
<xsl:value-of select="../TraderName"/>
<xsl:text>Trader Lato Carum</xsl:text>
</xsl:template>
<xsl:template match="response/result/doc/int[@name='TraderKey'][. = '62']>
<xsl:value-of select="../TraderName"/>
<xsl:text>Trader Samson Vero</xsl:text>
</xsl:template>
</xsl:stylesheet>
It doesn’t work at all and I’ve been hammering my head against a wall for the past hour. I don’t think this should be a difficult problem, what am I doing wrong?
Thanks
zoombini
You were on the right lines with the identity/copy pattern you had going on, you just need a bit of conditional output.
Demo at this XMLPlayground (see output source)