Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to find the position of a parent and grandparent etc. in XSL

645 views
Skip to first unread message

Slim

unread,
Aug 17, 2001, 1:18:41 PM8/17/01
to
I came to work with a full head of hair and now I am bald.

I am trying to find out the position of both my parant and grandparent from
my current node

What would be nice is <xsl:value-of select="../position()"/> and
<xsl:value-of select="../../position()"/> etc but this does not work.

I have had limited success with <xsl:value-of
select="count(../preceding-sibling::node())+1"/> for the parent, but I
cannot seem to get anything working for grandparent.

Thanks from the comfort of my padded cell.


Chris Newton

unread,
Aug 17, 2001, 8:14:52 PM8/17/01
to
Slim <Sli...@nowhere.co.uk> wrote...

> I came to work with a full head of hair and now I am bald.
>
> I am trying to find out the position of both my parant and
> grandparent from my current node

I don't know if this will help your particular case, but...

In using XSLT to render an article-style document with nested sections,
I wanted section titles of the form, e.g., "2.3 Title". Here the 2.3 is
based on the position of the current heading and subheading, as is
typical of formal reports.

My source document has an <article> element as its root. Within that, it
can have arbitrarily nested <section> elements, with the top level
representing major headings, the next level down subheadings, etc. I
used the following idea to get hold of the section numbers, which are
essentially the positions of the current element at each heading level.
This example is for a subheading, as in the "2.3 Title" above.

<xsl:variable name='secnum'>
<xsl:number count='/article/section'/>
</xsl:variable>
<xsl:variable name='subsecnum'>
<xsl:number count='/article/section/section'
from='/article/section'/>
</xsl:variable>

Hope it helps,
Chris


Thomas Broyer

unread,
Aug 17, 2001, 9:28:58 PM8/17/01
to
In <9ljjli$lue$1...@taliesin2.netcom.net.uk>, Slim wrote:
> I am trying to find out the position of both my parant and grandparent
> from my current node

position of a node is always relative to a node list. Which node list you
want the position to be relative to ? The sibling elements of the same
name? All the sibling elements, whatever their name? All the sibling nodes?
That's the reason why such a function doesn't exist in XPath.

> I have had limited success with <xsl:value-of
> select="count(../preceding-sibling::node())+1"/> for the parent,

That's the way to do it.

> but I cannot seem to get anything working for grandparent.

count(../../preceding-sibling::node()) + 1

fu2 ctx

Tom.

0 new messages