0

I would like to target all but the last iterations/nodes to give them a bottom border. Only the last iteration should not have a bottom border.

This is my XSLT now:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:decimal-format name="european" decimal-separator=',' grouping-separator='.'/>
  <xsl:template match="items">
  {$fallback = 0}
    <xsl:for-each select="item">
      <table align="center" cellpadding="0" class="resize" cellspacing="0" border="0" style="width:600px; background-color:#ffffff">
        <tr>
          <td align="center" style="border-bottom: 1px solid #000000;">
           <table cellpadding="0" cellspacing="0" style="width:190px" class="resize" align="left" bgcolor="#ffffff">
           {$fallback = 1}
             <tr>
               <td align="center" class="no_padding" style="padding:20px;">Do some code here
               </td>
             </tr>
           </table>
         </td>
       </tr>
     </table>
   </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Now I tried doing something like <xsl:if test="position() = last()"><td align="center" style="border-bottom: 1px solid #000000;"></xsl:if><xsl:if test="position() != last()"><td align="center"></xsl:if>

However I see nothing happening in my output HTML. Also if I just add <xsl:if test="position() = last()">asdjasdkkasdjasd</xsl:if> I do not see any text in the output.

Does anyone know how to fix this? Thanks in advance!

1 Answers1

-1

Use this to detect your last iteration:

<xsl:when test="position()=last()">
      <xsl:apply-templates />
      <xsl:text> last iteration </xsl:text>
</xsl:when>

EDIT: some explanation on this.

When having an XML like:

<items>
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
</items>

I can use a tool like xmlstarlet to select the last item:

xmlstarlet sel -t -m //item -v . -i "position()=last()" -o "X" -b -n xml2.xml

output:

1
2
3
4X

The XSLT can be generated for this command (see help of xmlstartlet ) and looks like:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common" version="1.0" extension-element-prefixes="exslt">
  <xsl:output omit-xml-declaration="yes" indent="no"/>
  <xsl:template match="/">
    <xsl:for-each select="//item">
      <xsl:call-template name="value-of-template">
        <xsl:with-param name="select" select="."/>
      </xsl:call-template>
      <xsl:choose>
        <xsl:when test="position()=last()">
          <xsl:text>X</xsl:text>
        </xsl:when>
      </xsl:choose>
      <xsl:value-of select="'&#10;'"/>
    </xsl:for-each>
  </xsl:template>
  <xsl:template name="value-of-template">
    <xsl:param name="select"/>
    <xsl:value-of select="$select"/>
    <xsl:for-each select="exslt:node-set($select)[position()&gt;1]">
      <xsl:value-of select="'&#10;'"/>
      <xsl:value-of select="."/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

This shows the XSLT code which is responsible for adding a X to the last item.

I also checked that changing the condition to position()!=last(), then the X appears at line 1,2 and 3.

Luuk
  • 12,245
  • 5
  • 22
  • 33