7

简化示例:

<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>

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

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

提前致谢!

4

4 回答 4

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>

然后你可以在你的第二个计算值作为参数过去

<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>

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

于 2013-09-27T12:02:50.843 回答
3

最简单的方法是稍微改变嵌套 -helper只生成文本节点并将其放入<xsl:attribute>调用模板中:

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

<xsl:template match="/>
  <myelem>
    <xsl:attribute name="myattr">
      <xsl:call-template name="helper" />
      <xsl:text>second calculated value</xsl:text>
    </xsl:attribute>
  </myelem>
</xsl:template>

这将设置myattr为“第一个计算值第二个计算值” - 如果您想要在“值”和“第二个”之间有一个空格,则必须在其中一个<xsl:text>元素中包含该空格

      <xsl:text> second calculated value</xsl:text>
于 2013-09-27T09:25:15.060 回答
0

尝试这个:

  <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:variable name="temp" select="@myattr"/>
      <xsl:attribute name="myattr">
        <xsl:value-of select="concat($temp, 'second calculated value')"  />
      </xsl:attribute>
    </myelem>
  </xsl:template>
于 2013-09-27T08:26:20.727 回答
0

虽然它或多或少是同一件事,但我更喜欢创建变量的更简洁的方式,而不是使用帮助模板。请注意,对于更复杂的情况,您仍然可以从 xsl:variable 中调用模板。

<xsl:template match="/">
  <myelem>
    <xsl:variable name="first">first calculated value </xsl:variable >
    <xsl:attribute name="myattr">
       <xsl:value-of select="concat($first, 'second calculated value')"/>
    </xsl:attribute>
  </myelem>
</xsl:template>
于 2016-06-09T10:22:54.670 回答