Re: [xsl] sorting and selecting the last value

Subject: Re: [xsl] sorting and selecting the last value
From: Ming Yu <myu@xxxxxxxx>
Date: Thu, 08 Aug 2002 15:31:33 -0700
Thanks a lot. It works just fine.

Ming

At 10:55 PM 8/8/2002 +0200, you wrote:
Hello Ming,

there is a difference between

<xsl:variable name="foo" select="bar"/>

and

<xsl:variable name="foo">
  <xsl:value-of select="bar"/>
</xsl:variable>

The first variable stores a node set, the second one a Result Tree Fragment (RTF). You have The same problem with your variable declaration. You can use an extension function to convert a RTF to a node set.

If you really only want to have the last node, you can do

<xsl:variable name="last-field-value">
  <xsl:for-each select="$full_path[contains($dbs_searched, @db)]/*">
    <xsl:sort select="."/>
    <xsl:if test="position()=last()">
      <xsl:copy-of select="."/>
    </xsl:if>
  </xsl:for-each>
</xsl:variable>

You create a RTF too, but can access the value through <xsl:value-of select="$last-field-value"/> and copy the node to the output via <xsl:copy-of select="$last-field-value"/>. You can't do node set functions on a RTF.

What are you trying with <xsl:for-each select="self::node()[not(.=following::node())]">??

If you sort them and you want to have the last one, you don't need that. If you want to have unique nodes, you can do <xsl:if test="not(. = following-sibling::*)"> to reach the same.

If you really need the current content of $field-value as you have it, only as node set, you should think about rearranging your code to use a "correct" grouping method (preceding/following-sibling axis or Muenchian Method). Don't do two things (sorting + grouping) in one step.

Regards,

Joerg

Ming wrote:
Hi,
I'm trying the access the last value of a sorted list but for some how,
the system produces the error:   Can not convert #RTREEFRAG to a
NodeList.
Here is my codes:
    <xsl:variable name='field_value'>
        <xsl:for-each select="$full_path[contains($dbs_searched,
@db)]/*">
                <xsl:sort select="."/>
                <xsl:for-each
select="self::node()[not(.=following::node())]">
                        <xsl:copy-of select="."/>
                </xsl:for-each>
        </xsl:for-each>
    </xsl:variable>
<xsl:variable name="lastval" select="$field_value[last()]" />
And the error poinrts to the last line: <xsl:variable name="lastval"
select="$field_value[last()]" />
But if I do <xsl:variable name='field_value'
select='$full_path[contains($dbs_searched, @db)]'/>  everything works
just fine.
Do you have some idea on what could cause the problem?
Thanks for your help.
Ming


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list


Current Thread