[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: reccursive sum ?

Subject: Re: reccursive sum ?
From: "Andrew Curry" <andrew.curry@xxxxxxxxxxxx>
Date: Tue, 2 Mar 2004 10:49:21 -0000
xslt sum for each
Something along these lines would work

<?xml version="1.0"?>

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="1.0">
<xsl:output method="xml" indent="yes"
xmlns:xalan="http://xml.apache.org/xslt";
  xalan:indent-amount="2"/>

<xsl:template match="/root">
  <!-- the id of the folder to count -->
  <xsl:variable name="dir-id" select="'dir1'"/>

  <xsl:variable name="count">
    <xsl:call-template name="count-files">
      <xsl:with-param name="dir" select="/root/folder[@id=$dir-id]"/>
    </xsl:call-template>
  </xsl:variable>

  There are <xsl:value-of select="$count"/> files under <xsl:value-of
select="$dir"/>.

</xsl:template>

<xsl:template name="count-files">
  <xsl:param name="dir"/>

  <!-- count the files in this dir -->
  <xsl:variable name="counter">
    <xsl:choose>
      <xsl:when test="$dir/file[@id]">
        <xsl:call-template name="count-subfiles">
          <xsl:with-param name="file" select="$dir/file[@id][1]"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:value-of select="$dir/@files + $counter"/>

</xsl:template>

<xsl:template name="count-subfiles">
  <xsl:param name="file"/>

  <!-- count the files in the related dir -->
  <xsl:variable name="x">
    <xsl:call-template name="count-files">
      <xsl:with-param name="dir" select="/root/folder[@id=$file/@id]"/>
    </xsl:call-template>
  </xsl:variable>

  <!-- count the files in the following-sibling files with dir ids-->
  <xsl:variable name="y">
    <xsl:choose>
      <xsl:when test="$file/following-sibling::file[@id]">
        <xsl:call-template name="count-subfiles">
          <xsl:with-param name="file"
select="$file/following-sibling::file[@id][1]"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:value-of select="$x + $y"/>

</xsl:template>

</xsl:stylesheet>

----- Original Message -----
From: "Ricaud Matthieu" <matthieu.ricaud@xxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Tuesday, March 02, 2004 10:39 AM
Subject:  reccursive sum ?


> For each "ME" node in LIST.xml, i calculate the number of "devoirs". To do
> so I must open each MATIERE.XML and making a count of all "DEVOIR" nodes.
>
> LIST.xml looks like this :
>
> <LISTE-ME>
> <ME id="0010"/>
> <ME id="0045"/>
> <ME id="0152"/>
> </LISTE-ME>
>
> Each ME has a description in his own file, for instance the file 0010.xml
> looks like this :
>
> <ME ref="0010">
> <FOURN label="anything">
> <DEVOIR name="anything">
> <DEVOIR name="anything">
> </FOURN>
> <FOURN label="anything">
> <DEVOIR name="anything">
> <DEVOIR name="anything">
> <DEVOIR name="anything">
> </FOURN>
> </ME>
>
> The XSL apply on LIST.xml looks like this :
>
> <xsl:template match="LISTE-ME">
> <xsl:for-each select="ME">
> <xsl:variable name="ThisMEid">
> <xsl:value-of select="@id"/>
> </xsl:variable>
> ME <xsl:value-of select="@id"/> :
> <xsl:call-template name="NbreDevoirsME">
> <xsl:with-param name="MEid" select="$ThisMEid"/>
> </xsl:call-template>
> devoirs <br/>
> <xsl:for-each/>
> </xsl:template>
>
> <xsl:template name="NbreDevoirsME">
> <xsl:param name="MEid"/>
> <xsl:variable name="MEfile">
> <xsl:value-of select="$MEid">.xml
> </xsl:variable>
> <xsl:value-of select="count(document($MEfile)/ME/FOURN/DEVOIR"/>
> </xsl:template>
>
> The Output would be here something like this :
>
> ME 0010 : 5 devoirs
> ME 0045 : 3 devoirs
> ME 0152 : 7 devoirs
>
> What i'd like is the total number of devoirs for all ME. ==> TOTAL = 15
> devoirs
>
> At this point I do not manage anymore :
>
> * I m pretty sure the solution must be a reccursive template, but i did
not
> find it ...
> * I thought about a way to calculate this sum :
> if I write <xsl:value-of select="5+3+7">, it gives 15.
> So I put in a variable "string" the expression to be calculated :
>
> <xsl:variable name="String">
> <xsl:for-each select="ME">
> <xsl:variable name="ThisMEid">
> <xsl:value-of select="@id"/>
> </xsl:variable>
> <xsl:call-template name="NbreDevoirsME">
> <xsl:with-param name="MEid" select="$ThisMEid"/>
> </xsl:call-template>
> <xsl:if test="position()!=last()">
> +
> </xsl:if>
> </xsl:for-each>
> </xsl:variable>
>
> Off course, when I write <xsl:value-of select="$String"/> the processor
> don't calculate it, it just dysplay the string.
> Is there a way to tell him, $string must be interpreted as a Xpath
> expression which you must calculate ?
>
>
>
>
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>


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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.