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

S

schaf

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
 
A

A. Bolmarcich

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top