繁体   English   中英

如何在xsl:attribute name =“width”中使用xsl:param值

[英]how to use xsl:param value in the xsl:attribute name=“width”

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

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

有什么建议?

谢谢

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

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

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

这里<xsl:value-of>元素没有关闭 - 这使得XSLT代码没有格式良好的xml。

方案

使用以下之一:

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

要么

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

为了可读性和紧凑性 ,尽可能使用上面的2 .

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

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

您的第一个替代方法失败,因为变量未在文本节点中展开。 您的第二个选项失败是因为您尝试调用<xsl:value-of-select="..."> ,而正确的语法是<xsl:value-of select="..."/> ,如部分使用xsl生成文本:标准中的value-of 您可以使用修复代码

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

或者,正如其他人所说,您可以使用属性值模板

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM