Altova Mailing List Archives


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

From: "Andrew Welch" <andrew.j.welch@--------->
To:
Date: 2/14/2007 10:33:00 AM
On 2/14/07, alex v. <alex.vett@xxxxxxxxx> 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

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.