我需要一个 XSLT 函数,它将返回我调用它的节点的 xpath。
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>
我尝试了上面的函数,但它抛出了一个错误:
XPDY0002:无法在此处选择节点:上下文项未定义
但是当我在命名模板中尝试这个时,我能够得到结果。这可以使用xslt:function
.