XSL Question tp xsl:for-each and xsl:variable

Discussion in 'XML' started by schaf@2wire.ch, May 27, 2005.

  1. Guest

    Hello NG !
    I have a big problem.
    I would like to go through a xml file in a xsl:for-each statement.
    for-each entry (ID) in the XML file i would like to call an
    xsl:function, which returns a number. This number i have to summarize
    during the whole for-each statement. At the end i should have the sum
    of the added numbers.

    Like:

    int sum;
    for(int i = 0; i <= 10; ++i)
    {
    sum += i;
    }

    I tried like this, but it does not work, becasue an xsl:variable could
    be set just once:

    <xsl:function name="rsh:bestanden">
    <xsl:param name="kursTyp" as="xs:string"/>
    <xsl:param name="studiumTyp" as="xs:string"/>
    <xsl:variable name="minPunkte"
    select="$study-def/studium[@studiumID=studiumTyp]/studiumTeil[@studiumTeilID=$kursTyp]/ECTSPunkte"/>
    <xsl:variable name="summeECTSPunkts"/>

    <xsl:for-each
    select="$study-def/studium[@studiumID=studiumTyp]/studiumTeil[@studiumTeilID=$kursTyp]/kursRef">
    <xsl:variable name="kursID" select="@ref"/>
    <xsl:variable name="kursNote"
    select="rsh:noteByKursID($kursID)"/>
    <xsl:if test="$kursNote != 'F'">
    =>??? $summeECTSPunkte = $summeECTSPunkte +
    rsh:punkteByKursID($kursID)
    </xsl:if>
    </xsl:for-each>


    Thanks for help
    regards
    Marcel
     
    , May 27, 2005
    #1
    1. Advertising

  2. On 2005-05-27, <> wrote:
    > I would like to go through a xml file in a xsl:for-each statement.
    > for-each entry (ID) in the XML file i would like to call an
    > xsl:function, which returns a number. This number i have to summarize
    > during the whole for-each statement. At the end i should have the sum
    > of the added numbers.

    [snip]

    Since you are using XSLT 2 (you used xls:function in the XSLT snippet),
    use a for-each to set the value of a xslt variable to a temporary tree
    whose elements contain the values to sum.

    Using parts of your snippet

    <xsl:variable name="tt-to-sum">
    <v>0</v> <!-- avoid having an empty tree -->
    <xsl:for-each select="*your select expression*">
    <xsl:variable name="kursID" select="@ref"/>
    <xsl:variable name="kursNote" select="rsh:noteByKursID($kursID)"/>
    <xsl:if test="$kursNote != 'F'">
    <v><xsl:value-of select="rsh:punkteByKursID($kursID)'/></v>
    </xsl:if>
    </xsl:for-each>
    </xsl:variable>

    The expression sum($tt-to-sum/*) is the sum of the values.
     
    A. Bolmarcich, May 27, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sparko
    Replies:
    0
    Views:
    421
    Sparko
    Jul 9, 2004
  2. Tjerk Wolterink

    xsl:for-each for each 3 elements problem

    Tjerk Wolterink, Nov 3, 2004, in forum: XML
    Replies:
    3
    Views:
    458
    Tjerk Wolterink
    Nov 3, 2004
  3. Konkee
    Replies:
    1
    Views:
    634
    Joris Gillis
    Jul 6, 2005
  4. Pat Maddox
    Replies:
    6
    Views:
    171
    Marcin Mielżyński
    Jan 20, 2006
  5. Igor Nn
    Replies:
    7
    Views:
    465
    Johnny Morrice
    May 28, 2011
Loading...

Share This Page