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
To use an XSLT parameterCreate an XsltArgumentList object and add the parameter using the AddParam method. Call the parameter from the style sheet.
XSLT <xsl:param> The <xsl:param> element is used to declare a local or global parameter. Note: The parameter is global if it's declared as a top-level element, and local if it's declared within a template.
You cannot - 'variables' in XSLT are actually more like constants in other languages, they cannot change value. Save this answer.
In order to pass multiple parameters to XSLT from BP you'll need to pass '=' as a separator.
<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:
<xsl:attribute name="width"><xsl:value-of select="$length"/></xsl:attribute>
or
<someElement width="{$length}"/>
For readability and compactness prefer to use 2. above, whenever possible.
You probably don't even need xsl:attribute
here; 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. 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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With