我需要爲我寫了一個xsl功能當前節點的XPath獲取當前節點的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。請幫忙。
Evem在修復了明顯的錯誤之後,生成的表達式通常會是正確的。爲什麼?因爲在一般情況下,這個XPath表達式選擇了許多元素 - 不僅僅是一個。 –
你會遇到任何問題,你有兩個同名的兄弟姐妹 - ''會給你兩個''節點的相同表達式。你應該找到一種方法來添加'[1]','[2]'等等,可能使用'position()'函數或'兄弟之前的軸'。 – Flynn1179