1. Computing & Technology

How to Use the Value-of Element in XSLT – XSLT Tutorial

Breaking Down XSLT - Part 3

From

The value-of element in XSLT allows you to locate and present specific data in XML code. When creating an XSLT style sheet, you develop an output stream that formats the data in XML code. The XSLT must point out how to display the information and where it is in the XML code. XSLT uses XPath for this function.

XML Sample Code

<catalog>
  <bat> James Smith Special Edition </bat>
</catalog>

XSLT Output Stream

XSLT creates definitions to display the data in XML code. In the XML sample above, James Smith Special Edition is the data string for the child element <bat>. To display this as part of a webpage created with XSLT, you can use the value-of element.

<?xml version="1.0"?>
<xsl: stylesheet version="1.0"
xmlns:xsl="http//www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>This is my XML data </h2>
<table>
  <tr><td></td></tr>
  <tr><td></td></tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

This code develops XSLT to produce an output stream. It begins with a declaration statement, adds a template element and uses HTML to create an output stream. At this point, the code only creates a table with two empty rows.

<xsl:value-of select="catalog/bat"/>

Using the value-of element creates a location with XPath syntax. The XPath provides a place in the XML code to locate the data. This element instructs the parser to look in the XML document and find 'catalog' then 'bat' and to display the data stream found there. Insert the value-of element into one of rows on the HTML table.

<?xml version="1.0"?>
<xsl: stylesheet version="1.0"
xmlns: xsl= http://http://www.w3.org/1999/XSL/Transform>
<xsl:template match="/">
<html>
<body>
<h2>This is my XML data </h2>
<table>
  <tr><td><xsl:value-of select="catalog/bat"/></td></tr>
  <tr><td></td></tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Value-of is just one element in XSLT that locates specific data via XPath. The next article in this series, Breaking Down XSLT, takes a look at 'for-each' to display XML data.

Breaking Down XSLT - XSLT Tutorials

©2012 About.com. All rights reserved.

A part of The New York Times Company.