This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Understanding XSLT templates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Understanding XSLT templates" Watch "Understanding XSLT templates" New topic

Understanding XSLT templates

Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

The XSLT transformation I'm working on is crucial for my project, and it's hampering progress as I'm not familiar with XSLT. I've managed to get my xsl stylesheet to do some of the things I want it to do, but not all. I'd just like to get it working without having to go though an XSLT book for now as I'm already reading two other books.

Anyway, to the point.

A. Combining apply-templates SOLVED: Okay, I've just discovered xsl:call-template.

I have the following:

<xsl:apply-templates select="docname/date/day"/>
<xsl:apply-templates select="docname/date/month"/>
<xsl:apply-templates select="docname/date/year"/>

And three separate matching templates. Surely there must be a way to just have one xsl:apply-templates statement.

B. Alternating rows

1. I need to read words <word>aWord</word> from an XML file, but I will never know how many words there will be
2. The words will go into an html table, a maximum of five per row.
3. Rows have alternating colors

If I had to write a program in Java which printed five words per line, I'd just do something like

So I'm assuming I could also use mod % 5 to create a new table row and perhaps some kind of boolean value to indicate whether we're working with odd or even rows for colors. Could someone help me out?

Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

Okay, I found an awesome XSLT tutorial here: zvon.

And so far I've come up with something like this:

My problem is that <tr> tags must open and close. I initially put </tr> and then <tr> in the when statement, but it didn't parse.

So now I'm attempting to figure out how to force it to go through five elements AND make only one row. Anyone?

Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

Just an update in case anyone was interested.

I found the answer on stackoverflow, but it took me a while to figure it out.

So here's my code:

I sat pondering for a while and couldn't figure out the math for $current-pos. So here's how it works:

1. Let's say we have 20 word nodes.
2. The first for-each will select nodes 1, 6, 11, and 16 and create a new row with each iteration.
3. Here's where I was confused: The above statement generates a tree of four nodes, so position() will actually return 1,2,3, and 4.
4. $current-pos must be then multiplied by 5, and a 1 must be added to match the real current position. 1 is 6, 2 is 11, 3 is 16. 4 is 21.
5. The next conditional for-each iterates through nodes [ 1-5][6-10][11-15][16-20]

and voila!

I agree. Here's the link:
subject: Understanding XSLT templates