【问题标题】:how to use xsl:param value in the xsl:attribute name="width"如何在 xsl:attribute name="width" 中使用 xsl:param 值
【发布时间】:2010-04-19 20:51:36
【问题描述】:

听起来很简单,但我的“简单”语法都不起作用:

<xsl:param name="length"/>
<xsl:attribute name="width">$length</xsl:attribute>
not
<xsl:attribute name="width"><xsl:value-of-select="$length"></xsl:attribute>

有什么建议吗?

谢谢

【问题讨论】:

  • 您可能希望格式化示例代码以使其更易于阅读。单击“编辑”链接,突出显示您的代码示例并单击代码格式按钮。
  • 问得好,但是你需要格式化你的代码(这次我为你做了)。 +1。请参阅我的答案,它提供了两种解决方案并推荐了其中更好的解决方案。 :)

标签: xslt width


【解决方案1】:

<xsl:attribute name="width">$length</xsl:attribute>

这将创建一个值为字符串$length 的属性。但是您想要名为 $length 的 xsl:param 的值。

<xsl:attribute name="width"><xsl:value-of-select="$length"></xsl:attribute>

这里&lt;xsl:value-of&gt; 元素没有关闭——这使得XSLT 代码不是格式良好的xml。

解决方案

使用以下方法之一:

  1. &lt;xsl:attribute name="width"&gt;&lt;xsl:value-of select="$length"/&gt;&lt;/xsl:attribute&gt;

  1. &lt;someElement width="{$length}"/&gt;

为了可读性和紧凑性,请尽可能使用 2. above

【讨论】:

  • 我假设您的意思是 xsl:value-of,而不是 xsl:value-of-select? :)
【解决方案2】:

你可能在这里甚至不需要xsl:attribute;最简单的方法是:

<someElement width="{$length}" ... >...</someElement>

【讨论】:

    【解决方案3】:

    您的第一个选择失败了,因为变量没有在文本节点中展开。您的第二种选择失败,因为您尝试调用&lt;xsl:value-of-select="..."&gt;,而正确的语法是&lt;xsl:value-of select="..."/&gt;,如标准中Generating Text with xsl:value-of 部分所述。您可以使用修复代码

    <xsl:attribute name="width"><xsl:value-of select="$length"/></xsl:attribute>
    

    或者,正如其他人所指出的,您可以使用attribute value templates

    <someElement width="{$length}" ... >...</someElement>
    

    【讨论】:

      猜你喜欢
      • 2013-02-26
      • 2013-04-05
      • 2015-04-08
      • 1970-01-01
      • 2014-11-04
      • 2020-10-09
      • 2016-05-26
      • 2023-02-19
      • 1970-01-01
      相关资源
      最近更新 更多