我需要获取我为其编写了 xsl 函数的当前节点的 xpath
<func:function name="fn:getXpath">
<xsl:variable name="xpath">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="concat($xpath, name())" />
<xsl:if test="not(position()=last())">
<xsl:value-of select="concat('/', $xpath)" />
</xsl:if>
</xsl:for-each>
</xsl:variable>
<func:result select="$xpath" />
</func:function>
但是当我运行这个时,我收到以下错误
file:///D:/test.xsl; Line #165; Column #63; Variable accessed before it is bound!
file:///D:/test.xsl; Line #165; Column #63; java.lang.NullPointerException
我正在使用 xalan 2.7.0。请帮忙。
在您的示例中,您尝试在定义本身中使用该变量,这是无效的。
看来您的意图是尝试修改现有值的值。然而,XSLT 是一种函数式语言,因此变量是不可变的。这意味着该值一旦定义就无法更改。
在这种情况下,你不需要那么复杂。您只需删除对变量本身的引用,您就会得到您需要的结果
<func:function name="fn:getXpath">
<xsl:variable name="xpath">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="name()"/>
<xsl:if test="not(position()=last())">
<xsl:value-of select="'/'"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<func:result select="$xpath" />
</func:function>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)