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 8199701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:13:30+00:00 2026-06-07T06:13:30+00:00

How to get the position of parent node in the complete document using xpath?

  • 0

How to get the position of parent node in the complete document using xpath?

say I have the following xml:

<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
</catalog>

and I have a XSLT to convert it into HTML, which is as follows (only snippet):

<xsl:template match="/">
<html>
  <body>  
  <xsl:apply-templates/>  
  </body>
  </html>
</xsl:template>

<xsl:template match="cd">
  <p>
    <xsl:number format="1. "/><br/>
    <xsl:apply-templates select="title"/>  
    <xsl:apply-templates select="artist"/>
  </p>
</xsl:template>

<xsl:template match="title">
  <xsl:number format="1" select="????" /><br/>
  Title: <span style="color:#ff0000">
  <xsl:value-of select="."/></span>
  <br />
</xsl:template>

What should I write at the place of ???? to get position of the parent cd tag in the document.
I have tried many expressions but nothing seems to be working. May be I am doing it altogether wrong.

  1. <xsl:number format="1" select="catalog/cd/preceding-sibling::..[position()]" />
  2. <xsl:number format="1" select="./parent::..[position()]" /><br/>
  3. <xsl:value-of select="count(cd/preceding-sibling::*)+1" /><br/>

I am interpreting 2nd as select current node’s parent axis and then tell the position of parent of the current node. Why is it not working? What is the correct way to do this.

FYI: I expect the code to print the position of parent cd tag of the current title tag uder processing.

Please can someone tell me how to do this.

  • 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-07T06:13:31+00:00Added an answer on June 7, 2026 at 6:13 am
    count(../preceding-sibling::cd) + 1
    

    You can run it here (note I removed the other number you were outputting, just for clarity).

    You were on the right lines, but remember that predicates are used only to filter nodes, not to return information. So:

    ../*[position()]
    

    …effectively says “find me the parent that has a position”. It returns the node, not the position itself. The predicate is just a filter.

    In any case there are pitfalls with using position(), and it can be used to return the position of the current, context node only – not another node.

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

Sidebar

Related Questions

With the following XML: <parent> <child>Stuff</child> <child>Stuff</child> </parent> Using XPath I query the child
I have the following XML document named testFix.fix <WriteFixedWidth Type=extract> <Position Start=1 Length=15 Name=Field1
I can't seem to get a div to absolutely position within its parent using
Is there any way to get mouse position relative to it's parent element? Let's
To get a parent of a node in a heap sort array, the calculation
If I have an XPathNavigator positioned on a node, how can I get an
I need to get position of element relative to its parent. position() is suppose
Possible Duplicate: Get xpath location of element in iframe(iframe from my domain) I have
Is it possible to get position inside the OpenLayers container from a lat/long point?
I am trying to get position inside a fling for a listview. No matter

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.