Re: [xsl] accessing last element of node set passed as parameter

Subject: Re: [xsl] accessing last element of node set passed as parameter
From: "paul morgan" <pmorg@xxxxxxxxx>
Date: Tue, 21 May 2002 09:28:45 -0700
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:


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"

    <xsl:template match="/">
        <xsl:element name="OUT">
            <xsl:apply-templates select="(IN/A)[1]" />

    <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:when test="($result/BAR)[last()] = false()">
                    <xsl:value-of select="0" />
                    <xsl:value-of select="(($result/BAR)[last()])/@i + 1" />

        <xsl:variable name="new">
            <BAR i="{$i}"/>

        <xsl:apply-templates select="following-sibling::*[1]">
            <xsl:with-param name="result" select="$result|xalan:nodeset($new)"/>



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"/>
>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
>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
>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
>selects all the BAR children of that node and
>selects the last of those children.
>But if $result contains three BAR nodes then
>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.

Outgrown your current e-mail service?
Get a 25MB Inbox, POP3 Access, No Ads and No Taglines with LYCOS MAIL PLUS.

 XSL-List info and archive:

Current Thread