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

Counting level of nodes beneath current (in XPATH)

Subject: Counting level of nodes beneath current (in XPATH)
From: "Ragulf Pickaxe" <jawxml@xxxxxxxxxxx>
Date: Tue, 11 Jan 2005 12:02:28 +0000
xpath counting
Hi all,

I have an XML with the following rules (simplified):

X -> Y+
Y -> Z X?
Z -> (textnode)


An example is like this: <X> <Y> <Z>lv 1</Z> <X> <Y> <Z>lv 2</Z> </Y> </X> </Y> <Y> <Z>lv 1</Z> <X> <Y> <Z>lv 2</Z> <X> <Y> <Z>lv 3</Z> </Y> </X> </Y> <Y> <Z>lv 2</Z> <X> <Y> <Z>lv 3</Z> </Y> </X> <X> <Y> <Z>lv 3</Z> <X> <Y> <Z>lv 4</Z> </Y> <Y> <Z>lv 4</Z> </Y> </X> </Y> </X> </Y> </X> </Y> </X>


What I am interested in, is the level of Y nodes.
If I wanted the top two levels, I could easily do this in xpath using count(ancestor::Y).
The problem is that I cannot readily count all descendants - count(descendant::Y) will not count the number of levels but the total number of Y nodes that are beneath the current Y node.
Is there a way for such counting in XPath?


I want all except the buttom 1, 2, 3 or more levels. The exact amount determined from a parameter.

I could have something like [not(X)] for leaf nodes, [not(X/Y/X)] for the the buttom 2 levels (leaf nodes

and one level up, counting from the leaf). But as the number of levels are to be determined dynamically,

this solution is not the best:

<xsl:template match="/">
 <xsl:choose>
   <xsl:when test="$lv='1'">
     <xsl:apply-templates select="Y[not(X)]" mode="One"/>
   </xsl:when>
   <xsl:when test="$lv='2'">
     <xsl:apply-templates select="Y[not(X/Y/X)]" mode="Two"/>
   </xsl:when>
   ...
   <xsl:otherwise>
     <xsl:apply-templates select="Y" mode="Zero"/>
   </xsl:otherwise>
 </xsl:choose>
</xsl:template>

<xsl:template match="Y" mode="One">
 <!-- Do some stuff -->
 <xsl:apply-templates select="Y[not(X)]" mode="One"/>
</xsl:template>


Thank you! Ragulf Pickaxe :-|

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/


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.