tags:

views:

250

answers:

3

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

+2  A: 

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

This will create an attribute with value the string $length. But you want the value of the xsl:param named $length.

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

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.

Dimitre Novatchev
I assume you mean xsl:value-of, not xsl:value-of-select? :)
markusk
@markusk: Thanks for noticing this. Corrected.
Dimitre Novatchev
A: 

You probably don't even need xsl:attribute here; the simplest way to do this is something like:

<someElement width="{$length}" ... >...</someElement>
Marc Gravell
A: 

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