[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: node-set processing

Subject: Re: node-set processing
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sun, 04 Dec 2005 21:00:25 -0500
xsl call template nodeset
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.