In XSLT, how to access an attribute in a variable which contains a tree fragment with only one node

339 views Asked by At

I work with DocBook XSLT and want to get the id attribute of an anchor. In DocBook the anchor is produced by the following XSLT template:

<xsl:template name="anchor">
  <xsl:param name="node" select="."/>
  <xsl:param name="conditional" select="1"/>

  <xsl:choose>
    <xsl:when test="$generate.id.attributes != 0">
      <!-- No named anchors output when this param is set -->
    </xsl:when>
    <xsl:when test="$conditional = 0 or $node/@id or $node/@xml:id">
      <a>
        <xsl:attribute name="id">
          <xsl:call-template name="object.id">
            <xsl:with-param name="object" select="$node"/>
          </xsl:call-template>
        </xsl:attribute>
      </a>
    </xsl:when>
  </xsl:choose>
</xsl:template>

I call the template from another template and store the result in a variable:

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

With

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

I can output the anchor as expected:

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

However, I also want to extract the value of the "id" attribute in order to create another element. As far as I understand the topic, the variable anchorOutput contains a tree fragment. So, I tried all variants of:

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

But even when I try:

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

I get 0 as a result. So, why it is not possible to get the id with this approach and how can I get it?

1

There are 1 answers

5
Martin Honnen On

The right path would be exsl:node-set($anchorOutput)/a/@id, I think. Or, if the a element is in the XHTML namespace, you need to declare a prefix for that namespace and use it, e.g. <xsl:value-of select="exsl:node-set($anchorOutput)/xhtml:a/@id" xmlns:xhtml="http://www.w3.org/1999/xhtml"/>.