RE: [xsl] Using <xsl:for-each> and position() to increase indent in each iteration (includes XSLT 2.0 solution)

Subject: RE: [xsl] Using <xsl:for-each> and position() to increase indent in each iteration (includes XSLT 2.0 solution)
From: "David Gadd" <David.Gadd@xxxxxxxxxxxxxxxxxxx>
Date: Fri, 13 May 2005 14:24:59 -0700
Thanks to everyone for your feedback on this, this is very helpful!

David Gadd

-----Original Message-----
From: Jay Bryant [mailto:jay@xxxxxxxxxxxx]
Sent: Thursday, May 12, 2005 9:05 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Using <xsl:for-each> and position() to increase
indent in each iteration (includes XSLT 2.0 solution)


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: [xsl] 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