Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 20th, 2005, 09:40 AM
schaf@2wire.ch
Guest
 
Posts: n/a
Default XSL Question tp xsl:for-each and xsl:variable

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

  #2  
Old July 20th, 2005, 09:40 AM
A. Bolmarcich
Guest
 
Posts: n/a
Default Re: XSL Question tp xsl:for-each and xsl:variable

On 2005-05-27, schaf@2wire.ch <schaf@2wire.ch> wrote:[color=blue]
> 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.[/color]
[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.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles