XSLT 函数获取节点的 xpath [英] XSLT function to get xpath to a node

查看:24
本文介绍了XSLT 函数获取节点的 xpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个 XSLT 函数,它将返回到它调用的节点的 xpath.

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

    <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:无法在此处选择节点:上下文项未定义

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

但是当我在命名模板中尝试这个时,我能够得到结果.这可以使用 xslt:function 实现.

But when I tried this in a named template I'm able to get the result. Can this be implemented using xslt: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.

根据 W3C XSLT 2.0 规范:

According to the W3C XSLT 2.0 spec:

"在样式表函数的主体内,焦点最初是不明确的;这意味着任何试图引用上下文项的尝试,上下文位置或上下文大小是不可恢复的动态错误.[XPDY0002]"

"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]"

在您的代码中:

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

解决方案:

为此函数添加一个参数——很自然,这将是节点,用于选择所需的 XPath:

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)

注意:显然,您必须将每个名称连接到一个 "/" 字符,并缩小表达式的范围,不要选择节点的任何兄弟节点,通过在谓词中使用位置.有关为节点构建 XPath 表达式的完整且正确的解决方案,请参阅我对此问题的回答:https://stackoverflow.com/a/4747858/36305

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

这篇关于XSLT 函数获取节点的 xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆