What Is the Purpose of xsl:value-of in XSLT?

In Extensible Stylesheet Language Transformations (XSLT), the <xsl:value-of> element extracts the text content or calculated string value of an XML node or expression and writes it into the result document. This article explores how <xsl:value-of> functions, its standard syntax, key attributes, behavioral differences across XSLT versions, and how it compares to alternative output instructions like <xsl:apply-templates> and <xsl:copy-of>.

Core Purpose and Functionality

The primary purpose of <xsl:value-of> is to convert the result of an XPath expression into a plain string and insert that string directly into the output stream (such as HTML, plain text, or another XML structure). Whenever an XML document contains data wrapped in tags or attributes that need to be displayed as raw text values, <xsl:value-of> is the primary tool used to retrieve and render that data.

Instead of copying markup, child nodes, or element structures, <xsl:value-of> extracts only the character data associated with the evaluated node or expression.

Basic Syntax and Examples

The element relies on the select attribute, which takes an XPath expression specifying the node, attribute, or computation to evaluate:

<xsl:value-of select="XPath_Expression" />

Consider the following input XML snippet representing a product catalog:

<product id="p101">
    <name>Wireless Mouse</name>
    <price currency="USD">29.99</price>
</product>

To extract the text inside the <name> element and the value of the currency attribute using an XSLT template:

<xsl:template match="product">
    <div class="product-item">
        <h3><xsl:value-of select="name" /></h3>
        <p>Price: <xsl:value-of select="price" /> (<xsl:value-of select="price/@currency" />)</p>
    </div>
</xsl:template>

The resulting transformation renders the text values cleanly within the defined HTML markup:

<div class="product-item">
    <h3>Wireless Mouse</h3>
    <p>Price: 29.99 (USD)</p>
</div>

Key Attributes and Parameters

While select is the most common attribute, <xsl:value-of> supports several configuration options depending on the transformation environment:

Behavioral Changes Across XSLT Versions

The behavior of <xsl:value-of> varies depending on the version of XSLT implemented:

<xsl:value-of> vs. Other Output Instructions

Understanding when to use <xsl:value-of> rather than alternative XSLT elements is essential for building clean stylesheets:

By isolating and outputting plain text values, <xsl:value-of> serves as one of the most foundational instructions in XSLT for constructing precise data presentations and structured reports.