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

Re: Using <xsl:for-each> and position() to increase i

Subject: Re: Using <xsl:for-each> and position() to increase indent in each iteration (includes XSLT 2.0 solution)
From: "Jay Bryant" <jay@xxxxxxxxxxxx>
Date: Thu, 12 May 2005 23:05:24 -0500
xsl for each position
Hi, David,

I've done similar things in FO thus:

<fo:block text-indent="{(position() - 1 ) *
.25}in"><xsl:apply-templates/></fo:block>

If you're not set on spaces for indentation, you could try:

<p style="text-indent: {(position() -1) * .25}in"><xsl:apply-templates/></p>

or (if you're defining styles somewhere):

<p class="indent{(position() -1)}"><xsl:apply-templates/></p>

If you are set on spaces, Aron's recursive template works for XSLT 1.0+.

If you can do XSLT 2.0, you can use the "x to y" feature of for-each to get
the spaces, thus:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="x">
    <html>
      <body>
          <xsl:for-each select="y">
            <p>
              <xsl:if test="position() &gt; 1">
                <xsl:for-each select="1 to (position() -
1)">&#160;&#160;&#160;</xsl:for-each>
              </xsl:if>
              <xsl:value-of select="."/>
            </p>
          </xsl:for-each>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

Tested with Saxon 8.

Jay Bryant
Bryant Communication Services

----- Original Message ----- 
From: "David Gadd" <David.Gadd@xxxxxxxxxxxxxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Thursday, May 12, 2005 6:47 PM
Subject:  Using <xsl:for-each> and position() to increase indent in
each iteration


Hi,

I'm new to the list and fairly new to XSL.

I am wanting to use the position() function to increase the indent in an
<xsl:for-each> iteration by 3 spaces, using the following logic, but
preferably without using a case statement:

position() == 1 > generate ""
position() == 2 > generate "&nbsp;&nbsp;&nbsp;"
position() == 3 > generate "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
position() == 4 > generate
"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
etc
etc

As a separate issue, when I use &nbsp; in my xsl template it seems to fail
(the &nbsp; disappears).

By googling I found a suggestion to use &#160; instead.

Is there a reason why &nbsp; is not working?

Thanks very much,

David Gadd

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.