How to use xsl: parameter value in xsl attribute: name = "width"

It sounds simple, but none of my “simple” 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?

thank

+5
source share
3 answers

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

This will create an attribute with a string value $length. But you need the xsl: param value with the name $length.

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

The element is <xsl:value-of>not closed here - this makes the XSLT code incorrect. xml.

Decision

Use one of the following actions:

  • <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 when possible.

+8
source

You probably don't even need to xsl:attributehere; the easiest way to do this:

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

Your first alternative fails because the variables are not expanded on text nodes. The second alternative fails because you are trying to invoke <xsl:value-of-select="...">, while the correct syntax <xsl:value-of select="..."/>, as described in Generating text using xsl: value- from, in the standard. You can fix your code using

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

or, as others have noted, you can use attribute value patterns :

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

All Articles