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

  • Home
  • SEARCH
  • 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 6945447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:26:13+00:00 2026-05-27T13:26:13+00:00

I need a XSLT function which will return me the xpath to the node

  • 0

I need a XSLT function which will return me the xpath to the node from which it called.

XML

    <root>
      <node>
        <subnode />
        <subnode />
        <subnode />
      </node>
      <node>
        <subnode>
          <subsubnode >
            <xsl:value-of select="fn:generateXPath()" />
          </subsubnode >
        </subnode>
      </node>
    </root>

XSL

    <xsl:template match="root/node/subnode/sub" >
        <xsl:value-of select="fn:generateXPath()" />
    </xsl:template>

    <xsl:function name="fn:generateXPath" >
      <xsl:for-each select="ancestor::*">
      <xsl:value-of select="name()" />
      </xsl:for-each>
      <xsl:value-of select="name()" /> 
    </xsl:function>

I tried with the above function but it throws an error:

XPDY0002: Cannot select a node here: the context item is undefined

But when I tried this in a named template I’m able to get the result. Can this be implemented using xslt:function.

  • 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-05-27T13:26:14+00:00Added an answer on May 27, 2026 at 1:26 pm

    I tried with the above function but it throws an error:

    XPDY0002: Cannot select a node here: the context item is undefined
    

    But when I tried this in a named template I’m able to get the
    result.

    According to the W3C XSLT 2.0 spec:

    “Within the body of a stylesheet function, the focus is initially
    undefined; this means that any attempt to reference the context item,
    context position, or context size is a non-recoverable dynamic error
    .
    [XPDY0002]”

    In your code:

    <xsl:function name="fn:generateXPath" >    
      <xsl:for-each select="ancestor::*">    
      <xsl:value-of select="name()" />    
      </xsl:for-each>    
      <xsl:value-of select="name()" />     
    </xsl:function>    
    

    there are a number of relative expressions that can only be evaluated against the context item (focus, current node), however there is no such defined by definition (see the quotation above) and thus you get the reported error.

    Solution:

    Add a parameter for this function — ir is natural that this would be the node, the XPath for selecting which is wanted:

    <xsl:function name="fn:generateXPath" as="xs:string" >
      <xsl:param name="pNode" as="node()"/>
    
      <xsl:for-each select="$pNode/ancestor::*">    
        <xsl:value-of select="name()" />    
      </xsl:for-each>    
      <xsl:value-of select="name($pNode)" />     
    </xsl:function>    
    

    and call this function in the following way:

    fn:generateXPath(someNode)
    

    Note: Obviously, you have to concatenate each name to a "/" character, and also narrow down the expression not to select any siblings of the node, by using positions within predicates. For a complete and correct solution that builds an XPath expression for a node, see my answer to this question: https://stackoverflow.com/a/4747858/36305

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

Sidebar

Related Questions

I have the following code that applies a XSLT style Test.Xml.xslTransform = function(xml, xsl)
Using xslt/xpath, I need to be able to select the two elements which have
I have a load of XSLT from a third party which i need to
Is there a URL encoding function in XSLT version 1? I need something similar
An XSLT-newbie problem: I need to substitute a text value in XML-file. All other
I need to perform a find and replace using XSLT 1.0 which is really
I'm writing an XSLT template that need to output a valid xml file for
I'm writing a template in xslt with xpath 1.0 and need to access the
I need to transform a XML. Usually I would use Saxon and a XSLT
Evidently MSXML6 doesn't support XSLT 2.0, at least not the max() function. I need

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.