xsl-list
[Top] [All Lists]

Re: reccursive sum ?

2004-03-02 03:49:21
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[(_at_)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[(_at_)id]">
        <xsl:call-template name="count-subfiles">
          <xsl:with-param name="file" select="$dir/file[(_at_)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[(_at_)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[(_at_)id]">
        <xsl:call-template name="count-subfiles">
          <xsl:with-param name="file"
select="$file/following-sibling::file[(_at_)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(_dot_)ricaud(_at_)cned(_dot_)fr>
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Sent: Tuesday, March 02, 2004 10:39 AM
Subject: [xsl] 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



<Prev in Thread] Current Thread [Next in Thread>