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:
select: Specifies the XPath expression to evaluate. In modern XSLT, omittingselectallows the element to compute the string value of its own enclosed content template.disable-output-escaping: Acceptsyesorno. When set toyes, special characters like<and&are written out directly without being converted into entities like<or&. This is primarily used when emitting pre-formatted HTML fragments, though it should be used cautiously to avoid malformed output.separator(XSLT 2.0+): Defines a delimiter string placed between items when theselectexpression evaluates to a sequence of multiple items or nodes.
Behavioral Changes Across XSLT Versions
The behavior of <xsl:value-of> varies depending on
the version of XSLT implemented:
- XSLT 1.0: If the
selectexpression evaluates to a node-set containing multiple nodes,<xsl:value-of>outputs only the string value of the first node in document order, discarding the rest. - XSLT 2.0 and XSLT 3.0: If the
selectexpression evaluates to a sequence of multiple nodes or atomic values,<xsl:value-of>outputs the values of all items in the sequence, separated by a single space by default (or by the value defined in theseparatorattribute).
<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:
- **
<xsl:value-of>vs.<xsl:apply-templates>**: Use<xsl:value-of>when you specifically want raw string data without invoking template rules on child elements. Use<xsl:apply-templates>when processing mixed content, nested tags, or unknown hierarchical structures where recursive matching is required. - **
<xsl:value-of>vs.<xsl:copy-of>**:<xsl:value-of>strips all child markup and returns only text.<xsl:copy-of>performs a deep copy, replicating the target element, its attributes, and all child nodes exactly as they appear in the source.
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.