[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: limiting preceding axis by ancestor

Subject: RE: limiting preceding axis by ancestor
From: "Michael Kay" <michael.h.kay@xxxxxxxxxxxx>
Date: Tue, 7 May 2002 23:23:37 +0100
xsl ancestor preceding
> I have a document where there are many instances of <foo>.
> Each <foo> may
> have many <bar> children.   <bar> is itself recursive to
> several levels.
>
> <foo>
> 	<bar>
> 	<bar>
> <foo>
> 	<bar>
> 		<bar>
> 			<bar>
>
> I want to test to see if any preceding <bar> has the same value as the
> current <bar> but limit the test to those <bar> elements are
> children of the
> immediate <foo> ancestor.

By "preceding" do you mean preceding in the sense of the preceding axis,
which excludes ancestors of the context node? I'll assume you do.

Using XPath 2.0 you could write

   preceding::bar intersect ancestor::foo//*

or

   preceding::bar except ancestor::foo/preceding::bar

If you do want to include ancestors of the context node, you could write

   ancestor::foo/descendant::bar[.<<current()]

which is probably going to be the most efficient as it's the only one that
doesn't look outside the foo subtree.

Several XSLT 1.0 processors have xx:intersect() and xx:difference()
extension functions which you could use to simulate the first two of these.

I can't immediately think of an XPath 1.0 solution that only looks in the
foo subtree. But there's an XSLT solution using xsl:number -

<xsl:variable name="thispos">
  <xsl:number level="any" from="foo"/>
</xsl:variable>

<xsl:for-each select="ancestor::foo//bar">
<xsl:variable name="thatpos">
  <xsl:number level="any" from="foo"/>
</xsl:variable>
<if test="$thatpos < $thispos">
  -- process this node --
</xsl:if>
</xsl:for-each>

Michael Kay
Software AG
home: Michael.H.Kay@xxxxxxxxxxxx
work: Michael.Kay@xxxxxxxxxxxxxx




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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.