• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Understanding XSLT templates

 
Ranch Hand
Posts: 117
Mac Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 117
Mac Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 117
Mac Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!



 
It's a tiny ad only because the water is so cold.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic