_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
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
ASKER
Sure, my XSLT code is as follows (slightly modified for readability):
<xsl:for-each select="/pub:root/pub:Chil d/pub:Chil dData">
<xsl:if test="position() mod 3=1">
<xsl:text disable-output-escaping="y es"><</ xsl:text>t r class="row"<xsl:text disable-output-escaping="y es">></ 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="y es"><</ xsl:text>/ tr<xsl:tex t disable-output-escaping="y es">></ 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
<xsl:for-each select="/pub:root/pub:Chil
<xsl:if test="position() mod 3=1">
<xsl:text disable-output-escaping="y
</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="y
</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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, I was able to get it working using the following code:
<xsl:variable name="num" select="count(/pub:root/pu b:Child/pu b:ChildDat a)"/>
<xsl:if test="$num mod 3=1">
<td width="180" height="255" class="right"> </td>
<td width="180" height="255" class="right"> </td>
<xsl:text disable-output-escaping="y es"><</ xsl:text>/ tr<xsl:tex t disable-output-escaping="y es">></ xsl:text>
</xsl:if>
<xsl:if test="$num mod 3=2">
<td width="180" height="255" class="right"> </td>
<xsl:text disable-output-escaping="y es"><</ xsl:text>/ tr<xsl:tex t disable-output-escaping="y es">></ xsl:text>
</xsl:if>
<xsl:variable name="num" select="count(/pub:root/pu
<xsl:if test="$num mod 3=1">
<td width="180" height="255" class="right"> </td>
<td width="180" height="255" class="right"> </td>
<xsl:text disable-output-escaping="y
</xsl:if>
<xsl:if test="$num mod 3=2">
<td width="180" height="255" class="right"> </td>
<xsl:text disable-output-escaping="y
</xsl:if>
your most welcome.
thanks for the grade & points.
SnowFlake
thanks for the grade & points.
SnowFlake
basicly I guess you can use the count() function somehow.
SnowFlake