[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

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




0
_Dan_
Asked:
_Dan_
  • 3
  • 2
1 Solution
 
SnowFlakeCommented:
can you show some of your XML & XSLT?
basicly I guess you can use the count() function somehow.

SnowFlake
0
 
_Dan_Author Commented:
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
0
 
SnowFlakeCommented:
I guess you could add at the end something like:

<xsl:if test="count(/pub:root/pub:Child/pub:ChildData) mod 3 = 1">
add two TD's and close the TR
</xls:if>
<xsl:if test="count(/pub:root/pub:Child/pub:ChildData) mod 3 = 2">
add a single TD's and close the TR
</xls:if>

it might be possible to assign the result of the count to a variable to save the need to get the /pub:root/pub:Child/pub:ChildData nodelist twice.
but lets try and get things to work first and to be efficiant on the next phase.

SnowFlake
0
 
_Dan_Author Commented:
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>
0
 
SnowFlakeCommented:
your most welcome.
thanks for the grade & points.
SnowFlake
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now