This question follows on from query XSLT: Sorting based on sum of values from other nodes
I have this piece of xslt (thanks to Demitre) which I’ve modified to receive a parameter ‘Gait’ which can have values ‘P’ (Pace), ‘T’ (Trot) or ‘A’ (All):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kOffspring" match="Horse" use="SireID"/>
<xsl:param name="Gait"/>
<xsl:template match="/*">
<xsl:apply-templates select="Sires/Sire">
<xsl:sort select="sum(key('kOffspring', ID)/*/Stakes)"
data-type="number" order="descending"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="Sire">
Sire <xsl:value-of select="concat(ID,' (', Name, ') Stakes: ')"/>
<xsl:value-of select="sum(key('kOffspring', ID)/*/Stakes)"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
In the above code is this piece
sum(key('kOffspring', ID)/*/Stakes
Is there a way to substitute the asterisk part with the name of a node in xml tree depending on what value was passed in for gait?
Super simplified xml is:
<t>
<Horses>
<Horse>
<ID>5</ID>
<Name>hrsE</Name>
<SireID>101</SireID>
<Pace>
<Stakes>100</Stakes>
</Pace>
<Trot>
<Stakes>300</Stakes>
</Trot>
</Horse>
</Horses>
<Sires>
<Sire>
<ID>101</ID>
<Name>srA</Name>
<LiveFoalsALL>117</LiveFoalsALL>
</Sire>
</Sires>
</t>
When $Gait is ‘A’ I want sum(key(‘kOffspring’, ID)/*/Stakes (look in all sub-nodes of Horse)
When $Gait is ‘P’ I want sum(key(‘kOffspring’, ID)/Pace/Stakes (look in the Pace node only to find Stakes)
When $Gait is ‘T’ I want sum(key(‘kOffspring’, ID)/Trot/Stakes (look in the Trot node only to find Stakes)
So this is super-simplified example. I’m trying to stop having to duplicate hundred lines of code to cater to different values of $Gait. I played around trying use variables but couldn’t see how to change the value of the node path when it is using a key in the path. I saw I could potentially use a choose statement in the ‘match=”/*”‘ template but that was only for sorting, I was still stuck when I got to the ‘Sire’ template – didn’t want to have to put ‘choose’ around all the multitude of ‘value-of select’ statements I have.
Thanks for any suggestions.
Regards,
Bryce Stenberg.
Just use