XSLT Position of Ancestor

Posted on 2005-05-03
Last Modified: 2009-07-29
I need to get the position of the ancestor when i am in a grand-child.
Say i am in <item2> within the <submenu>
I need to go a find the position of its <item1>
How do i do this?

            <title subNo="Yes">Jim</title>

Question by:Joana
    LVL 15

    Accepted Solution

    by: you mean count item1, if you are in context of item2?

    you can do it for instance this way: count(ancestor::item1/preceding-sibling::item1)

    check this XSLT:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="">
          <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
          <xsl:template match="/">
                <xsl:for-each select="//item2">
                      Item2=<xsl:value-of select="." /><br/>
                      Ancestor Position is <xsl:value-of select="count(ancestor::item1/preceding-sibling::item1)" />

    Expert Comment

    well done dualsoul


    Author Comment

    Sorry for taking so long, forgot i hadn't changed my email address fo new job.  Thanks heaps for the answer it was just what i was looking for.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    Title # Comments Views Activity
    XML problem with Internet Explorer 7 22
    modifying sharepoint search results 13 26
    SVG, jquery, menu 14 55
    Convert XSD to XML 3 59
    The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now