简体   繁体   English

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

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

It sounds easy, but none of my "easy" syntax worked: 听起来很简单,但我的“简单”语法都不起作用:

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

any suggestions? 有什么建议?

thanks 谢谢

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

This will create an attribute with value the string $length . 这将创建一个值为字符串$length的属性。 But you want the value of the xsl:param named $length . 但是你想要名为$length的xsl:param的值。

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

Here the <xsl:value-of> element is not closed -- this makes the XSLT code not well-formed xml. 这里<xsl:value-of>元素没有关闭 - 这使得XSLT代码没有格式良好的xml。

Solution : 方案

Use one of the following: 使用以下之一:

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

or 要么

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

For readability and compactness prefer to use 2. above , whenever possible. 为了可读性和紧凑性 ,尽可能使用上面的2 .

You probably don't even need xsl:attribute here; 你可能在这里甚至不需要xsl:attribute ; the simplest way to do this is something like: 最简单的方法是这样的:

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

Your first alternative fails because variables are not expanded in text nodes. 您的第一个替代方法失败,因为变量未在文本节点中展开。 Your second alternative fails because you attempt to call <xsl:value-of-select="..."> , while the proper syntax is <xsl:value-of select="..."/> , as described in the section Generating Text with xsl:value-of in the standard. 您的第二个选项失败是因为您尝试调用<xsl:value-of-select="..."> ,而正确的语法是<xsl:value-of select="..."/> ,如部分使用xsl生成文本:标准中的value-of You can fix your code by using 您可以使用修复代码

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

or, as others have noted, you can use attribute value templates : 或者,正如其他人所说,您可以使用属性值模板

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

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

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