Reputation: 1457
Why is using XML data to style HTML tags illegal? For example:
<li style="width:<xsl:value-of select="width"/>px">
Why can't I do this? Are there any alternative methods out there?
Upvotes: 24
Views: 21317
Reputation: 183
If you are referencing to an xml-tag
XML: <width>640</width>
XSLT: <li style="width:{width}px">
If you are referencing to an xml-tag-attribute:
XML: <box width="640" height="480">...</box>
XSLT:
<xsl:for-each select="box">
<li style="width:{@width}px; height:{@height}px;">
</xsl:for-each>
Upvotes: 0
Reputation: 338316
Why can't I do this?
<li style="width:<xsl:value-of select="width"/>px">
Because XSL is XML itself. And this is anything… but not XML.
You mean an Attribute Value Template:
<li style="width:{width}px">
or the explicit form, for more complex expressions:
<li>
<xsl:attribute name="style">
<xsl:choose>
<xsl:when test="some[condition = 'is met']">thisValue</xsl:when>
<xsl:otherwise>thatValue</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</li>
or dynamic attribute names (note that attribute value template in the name):
<li>
<xsl:attribute name="{$attrName}">someValue</xsl:attribute>
</li>
Additional note: Attributes must be created before all other child nodes. In other words, keep <xsl:attribute>
at the top.
Upvotes: 40
Reputation: 41569
Your original xsl is not well formed as you can't have the xsl tag inside another node.
I think you need to use xsl:attribute as follows:
<li>
<xsl:attribute name="style">
width:<xsl:value-of select="width"/>px;
</xsl:attribute>
</li>
Upvotes: 10