XSLT function to get xpath to a node

11,597

Solution 1

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

Solution 2

One reason that there's no standard function is that people want the path for different reasons:

Sometimes a/b/c/d is enough.

Some people want a[3]/b[5]/c[1]/d[2].

Some people want a path where the names don't contain namespace prefixes, so it has to be something like

*:a[namespace-uri()='abc']/*:b[namespace-uri='xyz'] etc.

Share:
11,597
rohit
Author by

rohit

Updated on June 30, 2022

Comments

  • rohit
    rohit almost 2 years

    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.

  • Michael Kay
    Michael Kay over 12 years
    Note also the useful abbreviation <xsl:value-of select="$pNode/ancestor::*/name()" separator="/"/>
  • Dimitre Novatchev
    Dimitre Novatchev over 12 years
    @MichaelKay: Yes, I would do it that way. Here I simply added the necessary changes, not delving deeper into the OP's code.
  • rohit
    rohit over 12 years
    I'm assigning the return to a variable. And i need to get the value from another XML of same schema. For this I have tried, <xsl:variable name="LookUpXml" select="document('sample2.xml')" /> <xsl:value-of select="$LookUpXml/saxon:evaluate($nodePath)" />. But this give me an error, XPTY0004: A sequence of more than one item is not allowed as the first argument of saxon:evaluate().