PDA

View Full Version : XSL Sort by Sum question


MJM2007
06-18-2007, 08:05 PM
I'm trying to build a leader board for a fishing club. The section I'm having trouble with is building the total length. The reason I'm having trouble is because each fish must meet a minimum length based on it's type to be counted toward the total. Here is a sample of the XML:

<season>
<anglers nextID="2">
<angler>
<anglerID>1</anglerID>
<name>Angler 1</name>
<fishCaught nextID="3">
<fish>
<recID>1</recID>
<fishID>1</fishID>
<length>8</length>
</fish>
<fish>
<recID>2</recID>
<fishID>1</fishID>
<length>12</length>
</fish>
<fish>
<recID>3</recID>
<fishID>2</fishID>
<length>13</length>
</fish>
<fish>
<recID>4</recID>
<fishID>2</fishID>
<length>9</length>
</fish>
</fishCaught>
</angler>
</anglers>
<fishTypes>
<fishType>
<fishID>1</fishID>
<name>Bass</name>
<minLength>9</minLength>
</fishType>
<fishType>
<fishID>2</fishID>
<name>Pickerel/Pike</name>
<minLength>12</minLength>
</fishType>
</fishTypes>
</season>

Here is the XSL I'm currently using where I have the minimum length hard coded to 9:

<xsl:template name="leadersForTotalLength">
<table>
<tr class="colHeader">
<td width="100">
Competitor
</td>
<td>
Length
</td>
</tr>

<xsl:for-each select="/year/anglers/angler">
<xsl:sort select="sum(fishCaught/fish/length[number(.) &gt;= 9])" data-type="number" order="descending"/>

<tr>
<td>
<xsl:value-of select="name"/>
</td>
<td align="right">
<xsl:value-of select="sum(fishCaught/fish/length[number(.) &gt;= 9])"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>

If I were able to compute the total length of fish meeting the minimum requirements I would expect the total to be 25.

I'm guessing it's something like:

sum(fishCaught/fish/length[number(.) &gt;= /season/fishTypes/fishType[fishID = ????? ]/minLength])

Any help would be greatly appreciated. Maybe there's an easier way to go about this.

MJM2007
06-20-2007, 02:44 PM
I solved my problem but I ended up using javascript to do it. I was hoping to do it with xsl only.