This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Confusion about preceding-sibling axis


I'm a little confused about the preceding-sibling axis (or at least my
experience of it). Hopefully someone can point out my mistakes...

My original problem that required the use of the preceding-sibling axis was
to select a unique list of elements (using the familiar
*[not(@name=preceding-sibling::*/@name)] XPath pattern). I got unexpected
results so I simplified the XML and XSLT to investigate what was happening
(particularly with respect to using the preceding-sibling axis).

Consider the following simplified source XML:

<?xml version="1.0"?>
<root>
  <level1 id="1">
    <level2>
      <item>first item</item>
      <item>second item</item>
      <item>third item</item>
    </level2>
  </level1>
  <level1 id="2">
    <level2>
      <item>fourth item</item>
      <item>fifth item</item>
      <item>sixth item</item>
    </level2>
  </level1>
</root>

To visualise how the preceding-sibling axis works, I wrote the following
XSLT:

<xsl:template match="root">
  <xsl:variable name="item_list" select="//item"/>  <html>
  <body>
    <xsl:for-each select="$item_list">
      <p><b><xsl:value-of select="."/></b><br/>
        <xsl:choose>
          <xsl:when test="count(preceding-sibling::*) &lt; 1">No preceding
siblings<br/></xsl:when>
          <xsl:otherwise>Preceding siblings are:
            <xsl:for-each select="preceding-sibling::*">
              <xsl:value-of select="."/>&#160;
            </xsl:for-each>
          </xsl:otherwise>
        </xsl:choose>
      </p>
    </xsl:for-each>
  </body>
  </html>
</xsl:template>

I first create a variable, item_list, to contain all item elements. I then
visit each element in this list, displaying both it and its preceding
siblings.

What I would expect to see as output is:

first
No preceding siblings

second
Preceding siblings are: first

third
Preceding siblings are: first second

fourth
Preceding siblings are: first second third

fifth
Preceding siblings are: first second third fourth

sixth
Preceding siblings are: first second third fourth fifth


What I actually see though is:

first
No preceding siblings

second
Preceding siblings are: first

third
Preceding siblings are: first second

fourth
No preceding siblings

fifth
Preceding siblings are: fourth

sixth
Preceding siblings are: fourth fifth


Why is this so?

Once I'd created the item_list variable and used this for the <xsl:for-each>
I expected all item elements to have the same ancestor and therefore all be
siblings of each other. The results suggest that the original document
hierarchy is being used instead.

Am I missing something really obvious here?!

Thanks,

Darren Scott


********************************************************************
      Visit our Internet site at http://www.rbsmarkets.com

This e-mail is intended only for the addressee named above.
As this e-mail may contain confidential or privileged information,
if you are not the named addressee, you are not authorised to
retain, read, copy or disseminate this message or any part of it.
********************************************************************

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]