Using XSLT with one type of variable
Is there a way to use just one way of using a variable within XSLT?
For example:
<xsl:variable name="myvar" select="title" />
Which selects the title from my xml, then I would like to use it within the template:
<h2 title="{$myvar}">{$myvar}</h2>
However it only shows the title attribute. And开发者_如何转开发 when I do it like this:
<h2 title="<xsl:value-of select='$myvar'>"><xsl:value-of select='$myvar'></h2>
It only works the other way around.
So my question:
Is there one way that works for both attribute and content?
Yes, you can avoid AVT:
<h2>
<xsl:attribute name="title">
<xsl:value-of select=$myvar/>
</xsl:attribute>
<xsl:value-of select=$myvar/>
</h2>
But I think it's simpler using AVT and xsl:value-of
properly:
<h2 title="{$myvar}">
<xsl:value-of select=$myvar/>
</h2>
XSLT uses XML syntax, which is why you can't have an xsl:value-of instruction inside an attribute: hence the AVT notation using curly braces. It would be nice if the language allowed curly braces inside text nodes as well, but it doesn't.
精彩评论