Link to home
Start Free TrialLog in
Avatar of _Dan_
_Dan_

asked on

XSLT Question (figuring out end position)

Hello,

Here is what I am trying to do.

I have a grid of items.  The grid is a HTML table.  After every third item I use the XSLT position element and mod (if the remainder after dividing by 3 is 0) to start a new table row so that the grid never has more than three items across.  The problem is that at the end of the grid I do not know what position the last element was and sometimes I am missing the one or two cells that would bring the final row to three across (if the total number of elements is not divisible by three, and I do not know the total number beforehand).

What I would like to do is have a value after the end of the for-each loop (that generates the grid) with the remainder of the final position value (after it has been divided by three).

So if the remainder is 1, then I can add two more table cells, if it is 2 then I can add one more, and if it is 0 then I can just close the table.

I tried doing this by setting a variable inside the for-each loop.  But this variable is out of scope when I try accessing it outside the loop, and I can't declare a top level variable and then change the value inside the loop either.

Does anyone have any suggestions as to how to tackle this problem?

Thanks in advance for any help and or advice.

- Dan




Avatar of SnowFlake
SnowFlake
Flag of Israel image

can you show some of your XML & XSLT?
basicly I guess you can use the count() function somehow.

SnowFlake
Avatar of _Dan_
_Dan_

ASKER

Sure, my XSLT code is as follows (slightly modified for readability):

<xsl:for-each select="/pub:root/pub:Child/pub:ChildData">
  <xsl:if test="position() mod 3=1">
    <xsl:text disable-output-escaping="yes">&lt;</xsl:text>tr class="row"<xsl:text disable-output-escaping="yes">&gt;</xsl:text>
  </xsl:if>  
  <td width="180" height="255" class="right">
    <!-- Table Cell Contents -->        
  </td>
  <xsl:if test="position() mod 3=0">
    <xsl:text disable-output-escaping="yes">&lt;</xsl:text>/tr<xsl:text disable-output-escaping="yes">&gt;</xsl:text>
  </xsl:if>
</xsl:for-each>

The XML data is in a tree.  So I'm basically pulling a bunch of information from each of the child elements on that tree.

Thanks for you help.

- Dan
ASKER CERTIFIED SOLUTION
Avatar of SnowFlake
SnowFlake
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of _Dan_

ASKER

Thanks, I was able to get it working using the following code:

     <xsl:variable name="num" select="count(/pub:root/pub:Child/pub:ChildData)"/>

     <xsl:if test="$num mod 3=1">
       <td width="180" height="255" class="right">&#32;</td>
       <td width="180" height="255" class="right">&#32;</td>
       <xsl:text disable-output-escaping="yes">&lt;</xsl:text>/tr<xsl:text disable-output-escaping="yes">&gt;</xsl:text>
     </xsl:if>
      
     <xsl:if test="$num mod 3=2">
       <td width="180" height="255" class="right">&#32;</td>
       <xsl:text disable-output-escaping="yes">&lt;</xsl:text>/tr<xsl:text disable-output-escaping="yes">&gt;</xsl:text>
     </xsl:if>
your most welcome.
thanks for the grade & points.
SnowFlake