[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] how to select nodes between nodes


Subject: RE: [xsl] how to select nodes between nodes
From: "Mario Michlits" <mario.michlits@xxxxxxxx>
Date: Mon, 14 Jan 2002 20:10:48 +0100

Hello Jeni
Thanx a lot for your answer. I tried it out and it seems to work. Just if it interests u, i got another proposal that works (just fancying which one I should use):

<xsl:template match="/rootelement">
<xsl:for-each select="comma">
<!-- Handle the case when there is no 
opening delimiter -->
<xsl:if test="position() = 1 and count
(preceding-sibling::*) &gt; 0">
<xsl:element name="output">
<xsl:copy-of 
select="preceding-sibling::*"/>
</xsl:element>
</xsl:if>

<!-- Extract elements between this 
delimiter and the next (thanks to Gennady Loskutov) -->
<xsl:variable name="list" 
select="following-sibling::*[count(. |
current()/following-
sibling::comma[1]/preceding-sibling::*) =
count(current()/following-
sibling::comma[1]/preceding-sibling::*)]"/>
<xsl:if test="count($list) &gt; 0">
<xsl:element name="output">
<xsl:copy-of 
select="$list"/>
</xsl:element>
</xsl:if>

<!-- Handle the case when there is no 
closing delimiter -->
<xsl:if test="position() = last() and count
(following-sibling::*) &gt; 0">
<xsl:element name="output">
<xsl:copy-of 
select="following-sibling::*"/>
</xsl:element>
</xsl:if>

</xsl:for-each>
</xsl:template>

Greetings Mario

-----Original Message-----
From: Jeni Tennison [mailto:jeni@xxxxxxxxxxxxxxxx]
Sent: Freitag, 11. Januar 2002 13:35
To: Mario Michlits
Cc: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] how to select nodes between nodes


Hi Mario,

> I use an xsl:for-each that selects <comma> Elements within a certain
> context. In this xsl:for-each I want to copy all  elements that come
> between this <comma> Element and the  next <comma> Element and so
> on.

It's unfortunately a little complicated in XSLT. One method is to
identify the following comma:

  <xsl:variable name="next-comma"
                select="following-sibling::comma[1]" />

And then identify all the following siblings of this comma that have
the $next-comma as a following sibling. You can work out whether an
element has $next-comma as a following sibling with:

  generate-id(following-sibling::comma[1]) = generate-id($next-comma)

So you can copy all the elements between this comma and the next with:

  <xsl:copy-of select="following-sibling::*
                         [generate-id(following-sibling::comma[1]) =
                          generate-id($next-comma)]" />

You can also achieve this grouping effect by stepping through the
following elements one by one with a recursive template. And as with
any grouping, you can use the Muenchian Method here - index all the
elements (aside from the comma elements) by their nearest preceding
comma element and use that. If you'd like me to explain either of
these techniques, let me know.


For interest, in XSLT 2.0, a solution would be:

  <xsl:for-each-group group-starting-with="comma">
    ...
    <xsl:copy-of select="current-group()[not(self::comma)]" />
    ...
  </xsl:for-each>

You could also use the $next-comma as above and then:

  <xsl:copy-of select="following-sibling::*[$next-comma >> .]" />

Or even, if you really felt like it:
  
  <xsl:copy-of select="following-sibling::*
                         [following-sibling::comma[1] == $next-comma]" />

Cheers,

Jeni

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


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



Current Thread
Keywords