[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

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?

Nope.


Can you even run position on a node-set?

Nope.


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
<Table>
   <Row>
         <Cell>Cell 1</Cell>
         <Cell>Cell 2</Cell>
   </Row>
   <Row>
          <Cell>This is the longest cell</Cell>
          <Cell>Cell 3</Cell>
   </Row>
</Table>

T:\ftemp>type kamal.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:output method="text"/>

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

<xsl:template name="find-longest">
  <xsl:param name="set"/>
  <xsl:param name="index" select="1"/>
  <xsl:param name="longest" select="0"/>
  <xsl:choose>
    <xsl:when test="$index > count($set)">
      <!--finished looking-->
      <xsl:value-of select="$longest"/>
    </xsl:when>
    <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])"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <!--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"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>xslt kamal.xml kamal.xsl con
24
T:\ftemp>



--
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.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Cancer Awareness Aug'05  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


Current Thread
Keywords