xsl-list
[Top] [All Lists]

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

2007-11-30 14:56:31
Gareth,

Don't worry, you just have it a little backwards. Remember that your
<xsl:template/> elements should match fairly generically (match="player"
in this case), and your <xsl:apply-templates/> elements should be where
you narrow down your selection. 


Right now you have this:

<xsl:apply-templates select="//players/player" mode="PositionList" />
<xsl:template match="player[not(position= preceding::position/.)]"
mode="positionList">


The match condition in the template is a pattern, which can only use the
child:: and attribute:: axes; this ensures that the selection remains
context-free. (If I understand correctly, this is because there is no
context node outside a template.) However, you always use
<xsl:apply-templates/> from within a template, so you do have a context
node there, and preceding:: actually refers to a set of nodes. So you
want this:

<xsl:apply-templates select="//players/player[not(position=
preceding::position/.)]" mode="PositionList" />
<xsl:template match="player" mode="positionList">


Also, preceding::position/. is the same as preceding::position, so you
can omit the /. safely.

Good luck! Hope that helps.

~ Scott


-----Original Message-----
From: Gareth Howells 
[mailto:subscriptions(_at_)gforce-industries(_dot_)co(_dot_)uk] 
Sent: Friday, November 30, 2007 3:34 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Problem with xsl:template using XSLT 1.0

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


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>