[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Problem with xsl:template using XSLT 1.0


Subject: Re: [xsl] Problem with xsl:template using XSLT 1.0
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Sat, 1 Dec 2007 22:45:40 +0530

Hi Gareth,
   I agree to Scott's observations.

Saying in slightly different words ...

You need to swap the things used in 'select' (of xsl:apply-templates
construct), and what used in 'match' (of xsl:template construct).

<xsl:apply-templates select=" ... selects a node list (or 'set') and
applies a template rule to each node in this list (the template rule
'match' pattern matches the nodes in the list).

On 12/1/07, Gareth Howells <subscriptions@xxxxxxxxxxxxxxxxxxxxxxx> wrote:
> Hello all,
>
> First off, thanks to everyone who replied with suggestions to my query
> regarding converting a string to upper or lower case. Unfortunately due to
> an email issue I have been unable until now to post back to the group.
> Thanks again for all your suggestions.
>
> My query this time is regarding the use of xsl:template and
> xsl:apply-templates instead of using xsl:for-each loops.
>
> First off, a little bit of background - I am required to extract data from
> an XML regarding a fantasy football league. The data I am interested in in
> this instance is a list of all of the clubs from which there are players
> in the league, and a list of the positions in which they play, with
> duplicates removed using the predicates not(club= preceding::club/.) and
> not(position= preceding::position/.). The structure of the data in the XML
> file is as follows:
>
> fantasy/players/player
>
> with each player element having
>
> @pid
> name
> club
> value
> position
>
> I am trying to display a pair of unordered lists, one containing the clubs
> with duplicates removed, one containing the positions with duplicates
> removed. I am using unordered lists because I would prefer to use bullet
> points rather than numbered items, however I am using xsl:sort to sort the
> data into alphabetical order.
>
> I currently have a working stylesheet which uses xsl:for-each to achieve
> this, the relevant code reading as follows:
>
> <ul>
> <xsl:for-each select="//players/player[not(club=
> preceding::club/.)]<xsl:sort select="club" order="ascending" />
> <li><xsl:value-of select="club" /></li>
> </xsl:for-each>
> </ul>
>
> <ul>
> <xsl:for-each select="//players/player[not(position=
> preceding::position/.)]">
> <xsl:sort select="position" order="ascending" />
> <li><xsl:value-of select="position" /></li>
> </xsl:for-each>
> </ul>
>
> As I say, that works perfectly. However it is a requirement for my
> coursework that I use templates rather than for-each loops. I can only
> assume that my understanding of templates is flawed, because while I can
> get very basic examples to work, I just can't seem to get this to work.
> The code I am using is as follows (so far I've only implemented the
> positions list using templates):
>
> <ul>
> <xsl:apply-templates select="//players/player" mode="PositionList" />
> </ul>
>
> within <xsl:template match="/"> </xsl:template>, followed by the template
> definition:
>
> <xsl:template match="player[not(position= preceding::position/.)]"
> mode="positionList">
> <xsl:sort select="position" order="ascending" />
> <li><xsl:value-of select="position" /></li>
> </xsl:template>
>
> For some reason, what's generated is a <ul> element containing the values
> of each of the four child elements of player, with no <li> elements at
> all, an example taken from the HTML source code generated:
>
> <ul>
>      J Lehmann
>      ARS
>      4.4
>      Goalkeeper
>
>      M Almunia
>      ARS
>      4.1
>      Goalkeeper
>
>      J Aghahowa
>      WIG
>      5.7
>      striker
>
>      D Cotterill
>      WIG
>      5.3
>      striker
> </ul>
>
> Can anyone suggest what I might be doing wrong? Apologies if this mail is
> a little long. If you need the entire source for the XSL page, let me
> know.
>
> Thanks in advance,
> Gareth
> DMU


-- 
Regards,
Mukul Gandhi


Current Thread
Keywords