xsl-list
[Top] [All Lists]

[xsl] Recursive template, pattern matching, summing result

2006-09-07 10:09:13
What I am doing is looping through records, grabbing the expenses
associated with the currrnet record with the document() function,
using substring() to test whether the expense contains "mileage". If
any (0,1 or more expenses) are "mileage", split it up (using fxsl
strSplit-to-Words) and get the number of miles and move to the next
record.

Now since I'm not looping through the expenses, but the records and
then the checking the expenses, the template can't simply call itself
for each expense that contains "mileage".

Currently, I can isolate the mileage but I'm not sure how to do this
twice, sum them up, and then pass the total along to the template.
Perhaps this is a job for xsl:key?

XSL goes: (Recursive template, passed only the first Record/calling itself)

<xsl:template mode="totalExpense" match="Record">
        <xsl:param name="mileage">0</xsl:param>
        <xsl:variable name="expenses"
select="document(concat($getXML,'table=Activity_Expenses&amp;key=actID&amp;val=',id))/Records/Record"
/>
        <xsl:choose>
                <xsl:when test="following-sibling::Record[1]">
                                <xsl:variable name="miles">
                                        <xsl:call-template 
name="str-split-to-words">
                                                <xsl:with-param name="pStr"
select="msxsl:node-set($expenses[substring(expense,1,7)='mileage']/expense)"
/>
                                                <xsl:with-param name="pDelimiters" 
select="' '" />
                                            <!-- a blank space-->
                                        </xsl:call-template>
                                </xsl:variable>
                                <xsl:value-of select="msxsl:node-set($miles)/*[2]" 
/><br />
                        <xsl:apply-templates mode="totalExpense"
select="following-sibling::Record[1]">
                                <xsl:with-param name="mileage" select="$mileage 
+  *the sum of
miles totalled in this iteration * " />
                        </xsl:apply-templates>
                </xsl:when>
                <xsl:otherwise>
                        Total mileage:          $<xsl:value-of 
select="format-number($mileage,'#,00')" />
                </xsl:otherwise>
        </xsl:choose>
</xsl:template>


$expenses contains:

<Records>
  <Record>
      <expense>Mileage: 10 @ .445/mi</expense> <!-- I need the 10 here-->
      <cost>4.45</cost>
   </Record>
   <Record>
      <expense>Mileage: 10 @ .445/mi</expense>
      <cost>4.45</cost>
   </Record>
</Records>

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>
  • [xsl] Recursive template, pattern matching, summing result, Steve <=