Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use xsl:param value in the xsl:attribute name="width"

Tags:

width

xslt

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

like image 326
Elena Avatar asked Apr 18 '10 21:04

Elena


People also ask

How do you use parameters in XSLT?

To use an XSLT parameterCreate an XsltArgumentList object and add the parameter using the AddParam method. Call the parameter from the style sheet.

What is param name in XSLT?

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.

How do you assign values from one variable to another variable in XSLT?

You cannot - 'variables' in XSLT are actually more like constants in other languages, they cannot change value. Save this answer.

How do I pass multiple parameters in XSLT?

In order to pass multiple parameters to XSLT from BP you'll need to pass '=' as a separator.


3 Answers

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

like image 126
Dimitre Novatchev Avatar answered Oct 21 '22 17:10

Dimitre Novatchev


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

<someElement width="{$length}" ... >...</someElement>
like image 35
Marc Gravell Avatar answered Oct 21 '22 18:10

Marc Gravell


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>
like image 43
markusk Avatar answered Oct 21 '22 17:10

markusk