Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8514407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:51:36+00:00 2026-06-11T04:51:36+00:00

I want to get all ancestors of current node: XML: <root> <item title=a> <item

  • 0

I want to get all ancestors of current node:

XML:

<root>
   <item title="a">
       <item title="b">               
           <item title="c"></item> <!--CURRENT-->
           <item title="d"></item>                 
        </item> 
       <item title="x">             
           <item title="y"></item> 
           <item title="z"></item>  
       </item>            
   </item> 
</root>

Result:

<item title="a">...</item>
<item title="b">...</item>

Edit:
Answers with axes ancestor are fine.
My problem was elsewhere, in XSLT

XSLT:

<xsl:variable name="curr" select="//item[@title = 'c']"></xsl:variable>
<xsl:variable name="test" select="$curr/ancestor::item"></xsl:variable>

<xsl:for-each select="$test/item">
<xsl:value-of select="@title"></xsl:value-of>
</xsl:for-each>

Returns:

bcdx

Edit2: for dimitre and for all who have a similar problem

All the answers to my question were good.

Just XSLT (up) returns to me a strange result and @Mads Hansen corrected me.

FINAL WORKING EXAMPLE:

XML:

<?xml version="1.0" encoding="utf-8"?>
<root>
   <item title="a">
       <item title="b">               
           <item title="c"></item> 
           <item title="d"></item>                 
        </item> 
       <item title="x">             
           <item title="y"></item> 
           <item title="z"></item>  
       </item>            
   </item> 
</root>

XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:variable name="curr" select="//item[@title = 'c']"></xsl:variable>
        <xsl:variable name="test" select="$curr/ancestor::item"></xsl:variable>

        <xsl:for-each select="$test">
            <xsl:value-of select="@title"></xsl:value-of>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Returns:

ab
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:51:37+00:00Added an answer on June 11, 2026 at 4:51 am

    Congradulations to Adam for a very quick first answer.

    Just to add a little detail:

    Your listed expected result does not match your words. the root element also an ancestor node and the document is also an ancestor node.

    ancestor::node()
    

    … will return a sequence in this order:

    1. item[@title='b']
    2. item[@title='a']
    3. the root element (a.k.a. the document element)
    4. the root node /

    To get the specific result you listed, you need:

    ancestor::item/.
    

    The effect of the /. is to change the ordering back to forward document order. The native order of ancestor:: is reverse document order.


    Update: Illustration of points made in the comment feed.

    This style-sheet (with OP’s input)…

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    
    <xsl:template match="/">
      <xsl:for-each select="//item[@title='c']">
        <xsl:value-of select="ancestor::item[1]/@title" />
        <xsl:value-of select="ancestor::item[2]/@title" />
      </xsl:for-each>  
    </xsl:template>         
    </xsl:stylesheet>
    

    … will output ‘ba’ illustrating the point that ancestor:: is indeed a reverse axis. And yet this style-sheet …

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    
    <xsl:template match="/">
      <xsl:for-each select="//item[@title='c']">
        <xsl:value-of select="(ancestor::item/@title)[1]" />
        <xsl:value-of select="(ancestor::item/@title)[2]" />
      </xsl:for-each>  
    </xsl:template>
    
    </xsl:stylesheet>
    

    … has the opposite result ‘ab’ . This is instructive because it shows that in XSLT 1.0 (not so in XSLT 2.0), the brackets remove the reverse nature, and it becomes a document ordered node-set.

    The OP has asked about a transform something like….

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    
    <xsl:template match="/">
      <xsl:for-each select="//item[@title='c']">
        <xsl:for-each select="ancestor::item">
          <xsl:value-of select="@title" />
        </xsl:for-each>
      </xsl:for-each>  
    </xsl:template>
    
    </xsl:stylesheet>
    

    This one returns ‘ab’ (in XSLT 2.0 it would return ‘ba’). Why? Because in XSLT 1.0, the xsl:for-each instruction ignores the reverse-ness of the axis and processes in document order (unless an xsl:sort instruction says otherwise).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want query a file xml with linq, i want get all descendat of
I want get all of the Geom objects that are related to a certain
I want to get all the Calendars, which are in my GoogleAccount, using the
I want to get all characters until there is an uppercase but from the
I want to get all photo fids albums. For example, I have albums and
I want to get all distribution lists & the members of those lists using
I want to get all the Applications that have intentlisteners to Intent.CATEGORY_HOME so basically
I want to get all the values(multiple) of a particular key.But i m getting
I want to get all values of a set interface in one go as
I want to get all the error messages out of the modelState without knowing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.