Re: [xsl] node-set processing

Subject: Re: [xsl] node-set processing
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sun, 04 Dec 2005 21:00:25 -0500
At 2005-12-05 10:20 +1100, Kamal Bhatt wrote:
For example, does a nodeset keep the position of the original source

Not even .. position() is only relative to the "current node set" as a result of the last push (<xsl:apply-templates>) or pull (<xsl:for-each>) ... not a node set variable and not the source node tree.

or will position give me the position in the node set?


Can you even run position on a node-set?


I tell my students in such a case when using XSLT 1.0 to just index into the node set using another variable that is a number, using a recursive template. Very much programming and quite unlike templating.

I hope the example below helps.

. . . . . . Ken

T:\ftemp>type kamal.xml
         <Cell>Cell 1</Cell>
         <Cell>Cell 2</Cell>
          <Cell>This is the longest cell</Cell>
          <Cell>Cell 3</Cell>

T:\ftemp>type kamal.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="";

<xsl:output method="text"/>

<xsl:template match="/Table">
  <xsl:call-template name="find-longest">
    <xsl:with-param name="set" select=".//Cell"/>

<xsl:template name="find-longest">
  <xsl:param name="set"/>
  <xsl:param name="index" select="1"/>
  <xsl:param name="longest" select="0"/>
    <xsl:when test="$index > count($set)">
      <!--finished looking-->
      <xsl:value-of select="$longest"/>
    <xsl:when test="string-length($set[$index])>$longest">
      <!--found new longest-->
      <xsl:call-template name="find-longest">
        <xsl:with-param name="set" select="$set"/>
        <xsl:with-param name="index" select="$index + 1"/>
        <xsl:with-param name="longest" select="string-length($set[$index])"/>
      <!--this isn't any longer-->
      <xsl:call-template name="find-longest">
        <xsl:with-param name="set" select="$set"/>
        <xsl:with-param name="index" select="$index + 1"/>
        <xsl:with-param name="longest" select="$longest"/>

T:\ftemp>xslt kamal.xml kamal.xsl con

Upcoming XSLT/XSL-FO hands-on courses:  Denver,CO March 13-17,2006
World-wide on-site corporate, govt. & user group XML/XSL training.
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Cancer Awareness Aug'05
Legal business disclaimers:

Current Thread