xsl-list
[Top] [All Lists]

Re: [xsl] how to know distance between two sibling nodes

2007-02-14 03:33:26
On 2/14/07, alex v. <alex(_dot_)vett(_at_)gmail(_dot_)com> wrote:
Hi all,
I would like to know distance between two sibling nodes.
One solution maybe is to count siblings between the two nodes.
So, I have a sequence of sibling node, for example:

<a>
<b>
<c>
<a>
<b>
<c>
<c>

and when I match element <c> I need to know the number of
preceding-sibling only until the first previous element <a>,
also it is:

<a>
<b>
<c> = 2
<a>
<c> = 1
<c> = 2

where digit is the distance from the first prevoius <a>.

Your examples don't match!  It makes it easier to reply if you provide
well-formed XML

I think that count(preceding-sibling::a[1]) is the solution, but it
seems don't work for me. Why this?

It's on the right track, try this:

select="count(.|preceding-sibling::*[. >> current()/preceding-sibling::a[1]])"

In words, this says: "Count all preceding-siblings that occurs after
the closest preceding-sibling <a> in document order" - the . includes
the current element itself to increase the count by one to get the
number you're after.

cheers
andrew

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>