caucho
 XSL value-of


Often, stylesheets need to grab data from the XML file and rearrange them in the template results. The previous examples used template matching for all processing, but couldn't grab data within the template pattern. xsl:value-of is the main XSL element to grab data from the XML.

xsl:value-of extracts data with an XPath select expression. select and the template's match pattern use the same syntax, but are used differently. For a match pattern, XSL has a current node and chooses the best matching pattern and its template. With a select pattern, XSL starts from a node and selects strings, numbers, or nodes from that node.

The following example uses xsl:value-of to extract the @name attribute to customize the message. The xsl:value-of adds text to the output.

default.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="hello">
  <xsl:text>Hello, </xsl:text>
  <xsl:value-of select="@name"/>
</xsl:template>

</xsl:stylesheet>

As the following XTP file shows, using xsl:value-of with XML attributes gives more power to the custom tags.

hello.xtp
Tag attributes: <hello name="Dolly"/>

Tag attributes: Hello, Dolly

StyleScript

Because xsl:value-of is so common, StyleScript has a special syntax for it. $(select) will write the text value of the select pattern to the output.

default.xsl
$template(hello) <<
Hello, $(@text)
>>

Summary

  • The XPath pattern @attr matches tag attributes.
  • xsl:value-of extracts values from the XML.
  • << ... >> trims initial and final linefeeds.
  • $(...) expands to <xsl:value-of select="..."/>

Copyright © 1998-2002 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.