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

Re: limitations of preceding-sibling and following-sibling

Subject: Re: limitations of preceding-sibling and following-sibling
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 22 Oct 1999 08:12:38 -0700

At 99/10/21 20:14 -0700, Earl Bingham wrote:
I'm hitting a limitation of xPath with
preceding-sibling and following-sibling.

Actually, what you are missing is appropriate use of the powerful construct ... it is so flexible to provide so many options that it isn't as limited as you claim.

which I'm having trouble creating the following:

 <li>first list element</li>
 <li>second list element</li>
<text>some text</text>
 <li>another list</li>
 <li>second anon list</li>

from the following:

  <bullet>first list element</bullet>
  <bullet>second list element</bullet>
  <normal>some text</normal>
  <bullet>another list</bullet>
  <bullet>second anon list</bullet>

I have the following stylesheet that will grab
the first group of bullets, but it will grab
all the bullets and not seperate the other
group of bullets.

That is because you asked for all.

These appears to be a limitation
of axis groupings.

Not at all. Again, the axis groups are very flexible, you just have to ask for what you want.

<xsl:for-each select="//normal|//bullet">
  <xsl:when select="self::normal">
  <xsl:when select="self::bullet">
    <xsl:if test="not(preceding-sibling::bullet)">
      <xsl:for-each select="self::bullet|following::bullet">
        <li><xsl:value-of select="."/></li>

It may help to view the axis illustration graphic on slide 39 of the free download preview available on our web site.

When you say in your stylesheet "following::bullet" you are asking for *all* elements that follow that are named "bullet". From your description of what you want, you selection is related to the following siblings.

Consider the differences:

   following-sibling::bullet - all following siblings named "bullet"
   following-sibling::*[self::bullet] - all following siblings named "bullet"
   following-sibling::bullet[1] - the first following sibling named "bullet"
   following-sibling::*[1] - the immediately following sibling regardless
                             of that sibling's name
   following-sibling::*[1][self::bullet] - the immediate following sibling
                                           only if it is named "bullet"

It is the last one that you need for what you want.

So you see there aren't "limitations" to these axes ... they were designed to be *very* flexible and we have a lot of power to ask for whatever it is we need.

A working example is below.

I hope this helps.

.......... Ken

T:\earl>type earl.xml <?xml version="1.0"?> <textItems> <bullet>first list element</bullet> <bullet>second list element</bullet> <normal>some text</normal> <bullet>another list</bullet> <bullet>second anon list</bullet> </textItems>

T:\earl>type earl.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

<xsl:output method="xml" indent="yes"/>

<xsl:template match="textItems">

<xsl:template match="bullet">
        <!--when there is an immediate sibling of the same name,
            then assume this node has already been addressed by
            the first of the contiguous siblings-->
    <xsl:when test="preceding-sibling::*[1][self::bullet]"/>
    <xsl:otherwise><!--at the first of a group of siblings-->
      <ul><!--"walk" along all sibling bullets for list items-->
        <xsl:apply-templates select="." mode="sibling-bullets"/>

        <!--place each of a group of adjacent siblings-->
<xsl:template match="bullet" mode="sibling-bullets">
  <li><xsl:apply-templates/></li> <!--put out this one-->
                                  <!--go to next one-->

<xsl:template match="normal">   <!--other stuff-->


T:\earl>call xsl earl.xml earl.xsl earl.out
T:\earl>type earl.out
<li>first list element</li>
<li>second list element</li>

  <text>some text</text>
<li>another list</li>
<li>second anon list</li>



G. Ken Holman                    mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.             http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999   (Fax:-0995)
Web site: XSL/XML/DSSSL/SGML services, training, libraries, products.
Practical Transformation Using XSLT and XPath      ISBN 1-894049-02-0
Next instructor-led training:  1999-11-08, 1999-11-09, 1999-12-05/06,
-                1999-12-07, 2000-02-27/28, 2000-05-11/12, 2000-05-15

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

Current Thread