XSLT function to get xpath to a node

前端 未结 2 1111
独厮守ぢ
独厮守ぢ 2021-01-14 19:37

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

XML

    
      
        

        
相关标签:
2条回答
  • 2021-01-14 19:55

    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 讨论(0)
  • 2021-01-14 20:10

    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.

    0 讨论(0)
提交回复
热议问题