XSLT - 有没有办法追加添加了<xsl:attribute>的属性?

The*_*ras 7 xml xslt

简化示例:

<xsl:template name="helper">
  <xsl:attribute name="myattr">first calculated value</xsl:attribute>
</xsl:template>

<xsl:template match="/>
  <myelem>
    <xsl:call-template name="helper" />
    <xsl:attribute name="myattr">second calculated value</xsl:attribute>
  </myelem>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

有没有办法让第二个第二个计算值附加到同一个值myattr结果节点中属性?

我已经看到如果目标属性在源xml中,可以使用属性值模板,但是我可以以某种方式引用我之前附加到结果节点的属性的值吗?

提前致谢!

Tim*_*m C 4

您可以采取的一种方法是将参数添加到帮助程序模板中,并将其附加到属性值中。

<xsl:template name="helper">
  <xsl:param name="extra" />
  <xsl:attribute name="myattr">first calculated value<xsl:value-of select="$extra" /></xsl:attribute>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

然后您可以将第二个计算值作为参数过去

<xsl:template match="/>
  <myelem>
    <xsl:call-template name="helper">
      <xsl:with-param name="extra">second calculated value</xsl:with-param>
    </xsl:call-template>
  </myelem>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

不过,您不必在每次调用时都设置参数。如果您不想附加任何内容,只需调用不带参数的帮助程序模板,并且不会将任何内容附加到第一个计算值。