This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Recursion Help with Summing Durations


Worked for me in both Saxon and Xalan 2.3.1

My input:

<timecard>
    <shift>
       <duration>P13M</duration>
    </shift>
    <shift>
       <duration>P43M</duration>
    </shift>
</timecard>

The Stylesheet:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:date="http://exslt.org/dates-and-times";
extension-element-prefixes="date">
  
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  
  <xsl:template match="timecard">
    <xsl:call-template name="total-durations">
      <xsl:with-param name="durations" select="shift/duration"/> 
    </xsl:call-template>
  </xsl:template>
  
  <xsl:template name="total-durations">
    <xsl:param name="durations"/>
    <xsl:choose>
      <xsl:when test="$durations">
        <xsl:variable name="first" select="$durations[1]"/>
        <xsl:variable name="rest">
          <xsl:call-template name="total-durations">
            <xsl:with-param name="durations"
select="$durations[position()!=1]"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:call-template name="date:add-duration">
          <xsl:with-param name="duration1" select="$first"/>
          <xsl:with-param name="duration2" select="$rest"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>P0M</xsl:otherwise>
    </xsl:choose>
  </xsl:template>

...
I used Jeni's XSLT impl of add-duration here...

</xsl:stylesheet>

The ouput:

P4Y8M



> -----Original Message-----
> From: owner-xsl-list@lists.mulberrytech.com [mailto:owner-xsl-
> list@lists.mulberrytech.com] On Behalf Of Seth Ladd
> Sent: Monday, April 29, 2002 2:28 PM
> To: xsl-list@lists.mulberrytech.com
> Subject: [xsl] Recursion Help with Summing Durations
> 
> Hello,
> 
> I am trying to use the EXSLT template add-duration.  I have a node-set
> of durations that I'd like to sum together.  My named template is not
> working, and I was hoping someone could give me some hints or tips on
> where to go to make this recursion work properly.
> 
> My environment:
> 
> Java 1.3.1
> Xalan2
> Cocoon 2.0.2
> 
> I've tested that I'm getting the correct node-set by using count().
> I've also hard-coded some values into add-duration to make sure it
> works.
> 
> I've been scanning documentation all day, and coming up empty.  I
might
> be getting tripped up with the difference between node-sets and RTFs,
> but that's where my XSLT gets fuzzy.  Any tips or suggestions would be
> greatly appreciated!
> 
> Thanks so much,
> Seth
> 
> My template:
> 
>     <xsl:template name="total-durations">
>         <xsl:param name="durations"/>
> 
>         <xsl:choose>
>             <xsl:when test="$durations">
>                 <xsl:variable name="first" select="$durations[1]"/>
>                 <xsl:variable name="rest">
>                     <xsl:call-template name="total-durations">
>                         <xsl:with-param name="durations"
>                                 select="$durations[position()!=1]"/>
>                     </xsl:call-template>
>                 </xsl:variable>
> 
>                 <xsl:call-template name="date:add-duration">
>                     <xsl:with-param name="duration1" select="$first"/>
>                     <xsl:with-param name="duration2" select="$rest"/>
>                 </xsl:call-template>
> 
>             </xsl:when>
>             <xsl:otherwise>P0M</xsl:otherwise>
>         </xsl:choose>
> 
>     </xsl:template>
> 
> I call it with this (while I am in the timecard element):
> 
> <xsl:call-template name="total-durations">
>     <xsl:with-param name="durations" select="shift/duration"/>
> </xsl:call-template>
> 
> And my original document looks like:
> 
> <timecard>
>     <shift>
>        <duration>P13M</duration>
>     </shift>
>     <shift>
>        <duration>P43M</duration>
>     </shift>
> </timecard>
> 
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]