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

RE: Return position in for-each

Subject: RE: Return position in for-each
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Fri, 23 Jul 2004 00:15:25 +0100
xsl for each position
In general, move the "test" condition into a predicate in the select
expression of the xsl:for-each. Change

<xsl:for-each select="x">
  <xsl:if test="y">

to

<xsl:for-each select="x[y]">

In your example it's not quite so easy because you are defining a variable
before the <xsl:if>, and the variable itself accesses the position. I don't
understand your logic well enough at this time of night to suggest a
rewrite.

Incidentally, 

  <xsl:variable name="x"><xsl:value-of select="y"/></xsl:variable>

should in 99.99% of cases be rewritten

  <xsl:variable name="x" select="y"/>

Michael Kay 

> -----Original Message-----
> From: Karl J. Stubsjoen [mailto:karl@xxxxxxxxxxxxxxxxxxxx] 
> Sent: 22 July 2004 23:46
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject:  Return position in for-each
> 
> Hello,
> 
> A disadvantage to an xsl:for-each is that the position() 
> increases by one
> for each step.  so, if an xsl:if test validates the current 
> step and says
> "Yes apply templates for this step", "don't apply templates 
> for this step",
> and you are interested in a positiion which reflects the 
> total number of
> "YES" steps... well you have a problem.  Take into consideration this
> for-each rule:
> 
> <xsl:for-each select="Entry">
> <xsl:variable name="element_name"><xsl:value-of
> select="concat('competitor_name_',position())"/></xsl:variable>
>   <xsl:if 
> test="string($BROWSER_VARS//ELEMENT[@name=$element_name]/.)">
>     <tr>
> 
> 	<!-- [[[ HERE WE ADD A COLUMN THAT HAS AN INCREMENTING 
> NUMBER ]]] -->
>       <td><xsl:value-of select="concat(position(),'.')"/></td>
> 
> 	<!-- [[[ HERE WE GET THE REST OF THE COLUMN DATA ]]] -->
>       <xsl:apply-templates select="Element" mode="PRINTER_FRIENDLY">
>          <xsl:with-param name="position"><xsl:value-of
> select="position()"/></xsl:with-param>
>       </xsl:apply-templates>
>     </tr>
>   </xsl:if>
> </xsl:for-each>
> 
> 
> Is there a work-around, or another way to accomplish an 
> incrementing number
> for the TD?
> 
> Karl

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.