This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: accessing last element of node set passed as parameter
- From: "paul morgan" <pmorg at lycos dot com>
- To: xsl-list at lists dot mulberrytech dot com
- Date: Tue, 21 May 2002 09:28:45 -0700
- Subject: Re: [xsl] accessing last element of node set passed as parameter
- Organization: Lycos Mail (http://www.mail.lycos.com:80)
- Reply-to: xsl-list at lists dot mulberrytech dot com
Hi David,
Below is a transform in which the two lines:
<xsl:copy-of select="($result/BAR)[last()]" />
<xsl:copy-of select="$result[last()]" />
produce identical output given the input:
<IN><A/><A/><A/><A/><A/><A/></IN>
Note: The transform is a condensed version of what I'm actually doing, so it may seem like a very silly way to do what it does (not that it wouldn't also seem very silly if you saw it in entirety). Oh, and I also realize that the union operator doesn't guarantee order, but it seems to work out with the Xalan processor.
<?xml version="1.0"?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xalan">
<xsl:template match="/">
<xsl:element name="OUT">
<xsl:apply-templates select="(IN/A)[1]" />
</xsl:element>
</xsl:template>
<xsl:template match="A">
<xsl:param name="result" select="/.." />
<!-- The two copy-of's generate the same output -->
<xsl:copy-of select="($result/BAR)[last()]" />
<xsl:copy-of select="$result[last()]" />
<xsl:variable name="prior" select="($result/BAR)[last()]" />
<xsl:variable name="i">
<xsl:choose>
<xsl:when test="($result/BAR)[last()] = false()">
<xsl:value-of select="0" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="(($result/BAR)[last()])/@i + 1" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="new">
<BAR i="{$i}"/>
</xsl:variable>
<xsl:apply-templates select="following-sibling::*[1]">
<xsl:with-param name="result" select="$result|xalan:nodeset($new)"/>
</xsl:apply-templates>
</xsl:template>
</xsl:transform>
--
On Tue, 21 May 2002 15:22:36
David Carlisle wrote:
>
>I think you need to show how you are setting your parameter.
>You say $result is
>
><BAR i=1/><BAR i=2/><BAR i=3/>
>
>but is it a node set consisting of three nodes or is it what you'd get
>from applying xx:node-set() to
><xsl;variable name="result">
><BAR i="1"/><BAR i="2"/><BAR i="3"/>
></xsl;variable>
>which is a node-set consisting of a single node (a root node0 which has
>three children.
>
>
>In the former to get <BAR i="3"/> 9if taht was last in doc order)
>you would go
>$result[last()]
>but if you do that on the second case you would get the same as
>$result as taking teh last element from a set of one doesn't do
>anything.
>
>You say
>
>
> This also seemed to work, judging by the fact that
>
> <xsl:copy-of select="($result/BAR)[last()]" />
> and
> <xsl:copy-of select="$result[last()]" />
>
> produced equivalent (looking) output.
>
>But they can not have done. If $result holds a root node then
>$result/BAR
>selects all the BAR children of that node and
>($result/BAR)[last()]
>selects the last of those children.
>
>But if $result contains three BAR nodes then
>
>$result/BAR
>
>selects all the BAR children of each element of $result, and that is
>empty so ($result/BAR)[last()] is similarly empty.
>
>> but using J's solution:
>
>> <xsl:variable name="prior" select="$result[last()]" />
>> Is there a "root" element here that J's solution creates?
>
>It seems most likely that there was a root node in $result, and so
>teh above sets prior to be teh same as result and
>
> <xsl:value-of select="$prior/TEXT/@i" />
>
>will give you the first i attribute of a TEXT child of result.
>
>David
________________________________________________________
Outgrown your current e-mail service?
Get a 25MB Inbox, POP3 Access, No Ads and No Taglines with LYCOS MAIL PLUS.
http://login.mail.lycos.com/brandPage.shtml?pageId=plus
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list