XSLT function to get xpath to a node

2019-02-19 20:49发布

问题:

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:

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



回答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.