Re: [xsl] Finding the following sibling of same type

Subject: Re: [xsl] Finding the following sibling of same type
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Sun, 29 Aug 2004 21:47:20 +0100
Hi Kenneth,

> Actually, the real problem is for me to write an XPath expression
> that will select all children of "main" from the first "first" upto
> (but exluding) the next "first". Hence my desire to locate the
> position of the next "first".

The easiest way to do this is to create a key that indexes all the
non-<first> children of the <main> element by their nearest preceding
<first> element:

<xsl:key name="first-group"
         match="main/*[not(self::first)]"
         use="generate-id(preceding-sibling::first[1])" />

Then you can get all the elements that 'belong to' the first <first>
element using:

  key('first-group', generate-id(/main/first[1]))

I'm using generate-id() here, but your actual XML might have an id
attribute or something similar that you could use to uniquely identify
the <first> elements instead.

If you're trying to transform into:

<main>
  <first>
    <second>
      <third>...</third>
    </second>
    <second>
      <third>...</third>
    </second>
  </first>
  <first>
    <second>...</second>
  </first>
</main>

and you're using XSLT 2.0, then you want to use the
<xsl:for-each-group> with group-starting-with. In XSLT 1.0 using keys
like the one above is probably the simplest approach.

Cheers,

Jeni

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

Current Thread