在 XSLT 中,如何访问包含只有一个节点的树片段的变量中的属性

问题描述

我使用 DocBook XSLT 并且想要获取锚点的 id 属性。在 DocBook 中,锚点由以下 XSLT 模板生成

<xsl:variable name="anchorOutput">
      <xsl:call-template name="anchor">
        <xsl:with-param name="conditional" select="0"/>
      </xsl:call-template>
</xsl:variable>

我从另一个模板调用模板并将结果存储在一个变量中:

<xsl:copy-of select="exsl:node-set($anchorOutput)"/>

<a id="d0e1292"></a>

我可以按预期输出锚点:

<xsl:value-of select="exsl:node-set($anchorOutput)[1]/@id"/>

但是,我还想提取“id”属性的值以创建另一个元素。 据我了解该主题,变量 anchorOutput 包含一个树片段。因此,我尝试了以下所有变体:

<xsl:value-of select="count(exsl:node-set($anchorOutput)[1]/@*)"/>

但即使我尝试:

mapply

结果我得到了 0。那么,为什么无法通过这种方法获取 id,我该如何获取

解决方法

我认为正确的路径是 exsl:node-set($anchorOutput)/a/@id。或者,如果 a 元素在 XHTML 命名空间中,您需要为该命名空间声明一个前缀并使用它,例如<xsl:value-of select="exsl:node-set($anchorOutput)/xhtml:a/@id" xmlns:xhtml="http://www.w3.org/1999/xhtml"/>