Computing Generated Text
Within a template, the xsl:value-of element can be used to compute generated text, for example, by extracting text from the source tree or by inserting the value of a string constant.
The xsl:value-of element computes the text by using a string expression that is specified as the value of the select attribute. String expressions can also be used inside attribute values of literal result elements by enclosing the string expression in curly braces ({}).
Let's suppose that you have a fragment of XML code that says this:
<link target="http://my.home.com/~simon">My Diary</link>
You want to convert the attribute into an element:
My Diary <address>http://my.home.com/~simon</address>
Listing 20.13 shows the XSL code to achieve this.
Listing 20.13 Using xsl:value-of
1: <xsl:template match="link"> 2: <xsl:apply-templates/> 3: <address> 4: <xsl:value-of select="attribute(link)"/> 5: </address> 6: </xsl:template>
You can just as easily go the other way and convert an element into an attribute. Imagine that you have the following XML code:
<image> <file>my-smiling-face.gif</file> <size width="100"/> </image>
and you want to convert it into this:
<graphic src="my-smiling-face.gif" width="100"/>
Listing 20.14 shows how you could achieve this.
Listing 20.14 Using an Attribute Value Template
1: <xsl:template match="image">
2: <graphic src="{file}" width="{size/@width}"/>
3: </xsl:template>