This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: How best to use recursion/call-templates?
- From: "Joerg Heinicke" <joerg dot heinicke at gmx dot de>
- To: <xsl-list at lists dot mulberrytech dot com>
- Date: Thu, 28 Feb 2002 00:21:04 +0100
- Subject: Re: [xsl] How best to use recursion/call-templates?
- References: <Pine.BSF.4.21.0202271731230.1834-100000@shell.cais.net>
- Reply-to: xsl-list at lists dot mulberrytech dot com
Hello David,
This is a normal grouping problem and therefore the Muenchian Method can be
used (http://www.jenitennison.com/xslt/grouping/muenchian.html).
For your XML:
<xsl:key name="rows" match="row" use="account"/>
<xsl:template match="xml">
<xsl:apply-templates select="row[count( . | key('rows',account)[1] ) =
1]"/>
</xsl:template>
<xsl:template match="row">
<span>
<xsl:value-of select="account"/>
<xsl:for-each select="key('rows',account)">
<a><xsl:value-of select="name"/></a>
</xsl:for-each>
</span>
</xsl:template>
But a comment to your XML-output:
In my eyes it's not good to have elements together with text-nodes (the
'32')
<span> 32
<a>smith</a>
<a>Jones</a>
</span>
So you have to look after whitespaces (for example by using normalize-space)
and can not use <xsl:value-of select="."/>, you must use <xsl:value-of
select="text()"/>. So together you need <xsl:value-of
select="normalize-space(text())"/> to get '32'. With a better XML-structure
you can avoid this.
Regards,
Joerg
> If I have this XML fragment:
>
> <xml>
> <row>
> <account>32</account>
> <name>Smith</name>
> </row>
> <row>
> <account>32</account>
> <name>Jones</name>
> </row>
> <row>
> <account> 35</account>
> <name>White</name>
> </row>
> </xml>
>
> ....then I'd like this output
>
> <span> 32
> <a>smith</a>
> <a>Jones</a>
> </span>
> <span> 35
> <a> White </a>
> </span>
>
> In other words: each account number is in its own <span> tag, and each
> name that has that account # is a link (<a>) inside that span tag. Since
> Jones and Smith both have the same account #, they are inside the same
> span tags. Since White has a different account number, there is a
> separate <span> tag.
>
> 1. How can I achieve this output?
> 2. Should I use recursion to achieve this?
> 3. Is this a procedural problem and should I rethink my approach?
>
> What I have tried: I have tried a number of call-template functions but I
> never seem to get the right result, primarily because I never quite nail
> down the right params to send. I've also tried using preceding-sibling to
> work through this, but I got deeper into trouble. I'd post my XSL but
> it's terribly convoluted now. I am admittedly lost. Any suggestions?
>
> David.
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list