Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSL value-of being repeated many times 1

Status
Not open for further replies.

cyssero

Technical User
May 26, 2008
2
AU
Hi everyone,

A newbie to this site and to XML in general, I am having a small issue with my stylesheet.

I have so far managed to use xsl:value-of to output a sum of some selected elements, and while the output is fine it tends to repeat itself far too many times.

Here is an example of my XSL:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
	<xsl:template match="runner">
    		<xsl:value-of select="sum(//points[preceding-sibling::house='Cyan'])" /><br />
			<xsl:value-of select="sum(//points[preceding-sibling::house='Magenta'])" /><br />
			<xsl:value-of select="sum(//points[preceding-sibling::house='Yellow'])" /><br />
			<xsl:value-of select="sum(//points[preceding-sibling::house='Black'])" /><br />
	</xsl:template>
</xsl:stylesheet>

Here is a snippet of my XML:

Code:
<carnivals>
    <carnival>
        <events>
            <maleunder13>
                <runner>
                    <lane>1</lane>
                    <position>3</position>
                    <time>11:44:47</time>
                    <house>Cyan</house>
                    <points>2</points>
                </runner>
                <runner>
                    <lane>2</lane>
                    <position>2</position>
                    <time>11:44:46</time>
                    <house>Magenta</house>
                    <points>3</points>
                </runner>
                <runner>
                    <lane>3</lane>
                    <position>1</position>
                    <time>11:44:41</time>
                    <house>Magenta</house>
                    <points>4</points>
                </runner>
            </maleunder13>
        </events>
    </carnival>
</carnivals>

The output appears to add the sum of the points correctly but if the sum of Cyan is 8, Yellow is 12 and Magenta is 6 the output is
8
12
6
8
12
6
8
12
6
8
12
6
8
12
6
[...]
-------------------------------

I am hoping that someone will be able to help me identify why the output is being repeated so many times when it is only required to be displayed once.

I appreciate any help, suggestions or advice anyone can provide. Thanks to anyone who can help!

PS. As mentioned before, new to XML & this website - both of which I seem to like a lot. Hopefully it won't be too long before I can offer some assistance to others :)
 
[0] The xsl and its template (only one) is loosely made. The match "runner" and "//points" has not much of a relationship. Hence, you can actually put anything like match="lane" or match="position" etc as long as the number of the latter is equal to the number of runner node. Hence it is not very logical and tightly made.

[1] Despite all defects, if you really want to use strictly xsl as shown, you can sure simply put this to control the repetition.
[tt]
<xsl:template match="runner">
[blue]<xsl:if test="position()=1">[/blue]
<xsl:value-of select="sum(//points[preceding-sibling::house='Cyan'])" /><br />
<xsl:value-of select="sum(//points[preceding-sibling::house='Magenta'])" /><br />
<xsl:value-of select="sum(//points[preceding-sibling::house='Yellow'])" /><br />
<xsl:value-of select="sum(//points[preceding-sibling::house='Black'])" /><br />
[blue]</xsl:if>[/blue]
</xsl:template>
[/tt]
[2] One way to make the logic and intention more clearly, you can simply do this.
[tt]
<xsl:template match="/">
<xsl:call-template name="get_sum" />
</xsl:template>
<xsl:template name="get_sum">
<xsl:value-of select="sum(//points[preceding-sibling::house='Cyan'])" /><br />
<xsl:value-of select="sum(//points[preceding-sibling::house='Magenta'])" /><br />
<xsl:value-of select="sum(//points[preceding-sibling::house='Yellow'])" /><br />
<xsl:value-of select="sum(//points[preceding-sibling::house='Black'])" /><br />
</xsl:template>
[/tt]
It executes once because document root occurs only once in any case.
 
Thank you very much for your help tsuji, following your example #2 I was able to achieve exactly what I am after. Further more I now have a better understanding of what I did wrong, particularly that that runner & points did not have a relationship.

Put simply, [2] makes a lot of sense and thank you for your knowledgeable response.

Thanks again,

Cyssero.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top