Re: [xsl] Node set

Subject: Re: [xsl] Node set
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 6 Feb 2001 10:09:08 +0000
Hi Yue,

> The reason I asked about ordered node set was that I have to group
> those title elements in the sample XML... (Now I'm clearly
> understand thata node set is not ordered.... ;-)) Say I may have 8
> or 10 title elements and finally, I'm going to display them within
> an HTML table, 4 on the left and 4 on the right column....without
> specific ordering, it's easy. And the bad news is the rule used to
> grouping them is "logic", so looks like nothing I can follow.... and
> as I said, the total number of title element is not guaranteed, if
> 4th element was missing, I have to move the 5th element to the left
> column! ;(

There's an XSLT solution to this kind of 'grouping by position'.  You
pick out the 'odd' title elements, apply templates to them, and get
them to output a row containing themselves and their 'even' partner
(if there is one).

Picking out the odd titles involves looking at their position() and
doing a calculation on it - if you do position() mod 2, you'll get 1
for odd items and 0 for even items - so you can pick out your odd
title elements with:

  <xsl:apply-templates select="title[position() mod 2 = 1]" />

Now have a template that matches title elements (it'll only be applied
to the odd ones) and outputs a row containing themselves and their
immediately following title element:

<xsl:template match="title">
   <tr>
      <td><xsl:value-of select="." /></td>
      <td><xsl:value-of select="following-sibling::title[1]" /></td>
   </tr>
</xsl:template>

This might not do precisely what you want it to do, but the
principle's there.  If you give a better sample of what your source
XML looks like (and what you want the table to look like in the end)
then we can probably give a more specific solution if you need it.

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread