This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes XSLT loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XSLT loop" Watch "XSLT loop" New topic
Author

XSLT loop

Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Hello,

I am trying to do a loop using XSLT and want to put a div tag around the content for each 10th row. I need the div tag around the content because I am going to use the content in a text scroller script.

I thought about doing something like this:

1) Increment parameter value (called counter) for each loop.

2) Test if position() = 1 or counter = 10. If true, then put a div start tag at the beginning and an end div tag at the end of the content.

3) If clause 2 is true, set counter variable to 0 and repeat above;

The code below doesn�t work as it isn�t valid html markup at runtime. Is there anyone who can correct the code below or do I have to use javascript to build up the code using a StringBuffer?


Thanks in advance...

[ December 03, 2008: Message edited by: Jeppe Sommer ]
[ December 03, 2008: Message edited by: Jeppe Sommer ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

But if I understand that code correctly, it's more like wrapping each group of ten rows into a <div> element. It certainly doesn't wrap each tenth row with <div> and </div> tags because it precedes the first row with an opening tag and doesn't output the closing tag until after the tenth row.
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Yes you are right. I was a bit unclear.

I want to wrap each group of ten rows into a <div> element.

Thanks in advance.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

When you want to do "for (int i=0; i<max, i++)" in XSLT, you're going to need to do it recursively rather than iteratively. Let's start with a template to output some rows inside a <div> element:
Now add some code to call that for the first ten rows:
That goes in your main code, not inside the tenrows template. Now the recursive part: have the tenrows template call itself for the next ten rows:

Now this isn't quite good enough because we need to stop when we get to the end. We need to wrap that last bit in an <xsl:if> element which only executes it if there's more rows to come. So that I don't do the whole thing for you, can I leave that for you?

Note: only typed into this box, not even checked for validity ;)
[ December 03, 2008: Message edited by: Paul Clapham ]
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Thanks, it all works now. Great support ;-)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: XSLT loop
 
Similar Threads
XSLT differences Java 1.4.2 to Java 1.6 ?
xsl
need to generate some numbers using loop, the number of times condition met
need to generate some numbers using loop, the number of times condition met
NBSP in Html output