xml editor

Supported platforms

Compatible with Windows7 & Mac OS X Snow Leopard

Ready for data server software
[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.


 In
which I'm having trouble creating the following:

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


from the following:


<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>

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

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"
                version="1.0">

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

<xsl:template match="textItems">
  <result>
    <xsl:apply-templates/>
  </result>
</xsl:template>

<xsl:template match="bullet">
  <xsl:choose>
        <!--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"/>
      </ul>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

        <!--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:apply-templates
            select="following-sibling::*[1][self::bullet]"
            mode="sibling-bullets"/>
</xsl:template>

<xsl:template match="normal">   <!--other stuff-->
  <text><xsl:apply-templates/></text>
</xsl:template>

</xsl:stylesheet>

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

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

</result>

T:\earl>

--
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
Keywords
XML Editor | XML Author | WYSIWYG Editors | Schema Editor | XSD Documentation | XSL/XSLT Editor | XQuery | XML Databases | SVN Client
© 2002-2011 SyncRO Soft Ltd. All rights reserved. | Sitemap | Privacy Policy | This website was created & generated with <oXygen/>®XML Editor