4

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.

Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
rohit
  • 953
  • 5
  • 15
  • 31

2 Answers2

8

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

Community
  • 1
  • 1
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • Note also the useful abbreviation `` – Michael Kay Dec 14 '11 at 15:18
  • @MichaelKay: Yes, I would do it that way. Here I simply added the necessary changes, not delving deeper into the OP's code. – Dimitre Novatchev Dec 14 '11 at 15:33
  • 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, . But this give me an error, XPTY0004: A sequence of more than one item is not allowed as the first argument of saxon:evaluate(). – rohit Dec 15 '11 at 09:52
8

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.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164