Reputation: 59
I'm working on a school assignment and I've been completely stumped with this step.
I need to find the sum of all elements who attribute value falls between two numbers, in this case greater than 9 and less than 18. I've spent an hour trying different combinations and done of them work, I can't find any example in the book which has this. Can someone help point out what I'm doing wrong or nudge me i the right direction?
Here's the snippet of the XML and XSLT.
<course>
<par holeNumber="1">4</par>
<par holeNumber="2">4</par>
<par holeNumber="3">5</par>
<par holeNumber="4">3</par>
<par holeNumber="5">4</par>
<par holeNumber="6">4</par>
<par holeNumber="7">5</par>
<par holeNumber="8">3</par>
<par holeNumber="9">4</par>
<par holeNumber="10">4</par>
<par holeNumber="11">5</par>
<par holeNumber="12">4</par>
<par holeNumber="13">3</par>
<par holeNumber="14">4</par>
<par holeNumber="15">5</par>
<par holeNumber="16">4</par>
<par holeNumber="17">3</par>
<par holeNumber="18">4</par>
</course>
<xsl:template match="par">
<td><xsl:value-of select="." /></td>
<xsl:choose>
<xsl:when test="./@holeNumber">
<td class="sub"><xsl:value-of select="sum(@holeNumber > 9)" /></td>
<td class="final"></td>" /></td>
<td class="final"></td>
</xsl:when>
<xsl:when test="1">
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
Upvotes: 2
Views: 1755
Reputation: 891
The following XSLT when applied to the sample input produces the desired result of 32
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="/">
<xsl:value-of select="sum(course/par[@holeNumber > 9 and @holeNumber < 18])"/>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Reputation: 52878
/course/par[@holeNumber <= 18 and 9 <= @holeNumber]
You can use this in your match
, or you can use it just to get your sum:
sum(/course/par[@holeNumber <= 18 and 9 <= @holeNumber])
NOTE: This is using <=
instead of just <
to get both 18 and 9. Remove the =
if you don't want them included.
Upvotes: 2