XSLT Value-of
The xsl:value-of element is one of the most frequently used XSLT instructions. It selects a node using an XPath expression and outputs its text content into the result document. Whenever you need to extract and display a value from XML, xsl:value-of does the job.
Basic Syntax
<xsl:value-of select="XPath-expression"/>
The select attribute contains an XPath expression that points to the node whose value you want. The instruction is self-closing — it produces output but has no child content of its own.
Selecting Element Content
XML Source
<product> <name>Laptop Stand</name> <price>1299</price> <brand>Ergodesk</brand> </product>
XSLT
<xsl:template match="product"> <p>Product: <xsl:value-of select="name"/></p> <p>Brand: <xsl:value-of select="brand"/></p> <p>Price: <xsl:value-of select="price"/></p> </xsl:template>
Output
Product: Laptop Stand Brand: Ergodesk Price: 1299
Selecting Attribute Values
Prefix the attribute name with @ to select its value.
XML Source
<employee id="E042" department="Finance"> <name>Harini Subramanian</name> </employee>
XSLT
<xsl:template match="employee"> <p>ID: <xsl:value-of select="@id"/></p> <p>Dept: <xsl:value-of select="@department"/></p> <p>Name: <xsl:value-of select="name"/></p> </xsl:template>
Output
ID: E042 Dept: Finance Name: Harini Subramanian
Selecting from Parent or Sibling Nodes
<!-- Current node's text --> <xsl:value-of select="."/> <!-- Parent element's text --> <xsl:value-of select=".."/> <!-- A sibling element --> <xsl:value-of select="../siblingName"/>
Using XPath Functions with value-of
<!-- Convert text to uppercase -->
<xsl:value-of select="translate(name, 'abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
<!-- Count child elements -->
<xsl:value-of select="count(items/item)"/>
<!-- String length -->
<xsl:value-of select="string-length(description)"/>
<!-- Concatenate strings -->
<xsl:value-of select="concat(firstName, ' ', lastName)"/>
disable-output-escaping
By default, xsl:value-of escapes special characters — < becomes < in the output. If your XML contains stored HTML (like in a CDATA section) and you want the HTML to render, set disable-output-escaping to "yes".
<xsl:value-of select="description" disable-output-escaping="yes"/>
xsl:value-of vs xsl:copy-of
| Instruction | Outputs | Use When |
|---|---|---|
| xsl:value-of | Text string only | You want just the text value of a node |
| xsl:copy-of | Full node tree with markup | You want to copy nodes including child elements |
<!-- Outputs only "Hello World" --> <xsl:value-of select="greeting"/> <!-- Outputs <greeting>Hello World</greeting> with the tag --> <xsl:copy-of select="greeting"/>
Complete Working Example
XML: invoice.xml
<invoice id="INV-2024-001" date="2024-03-15">
<customer>Meena Sundar</customer>
<items>
<item><name>USB-C Hub</name><qty>2</qty><unitPrice>850</unitPrice></item>
<item><name>Monitor Stand</name><qty>1</qty><unitPrice>1200</unitPrice></item>
</items>
<total>2900</total>
</invoice>
XSLT
<xsl:template match="/">
<html><body>
<h2>Invoice <xsl:value-of select="invoice/@id"/></h2>
<p>Date: <xsl:value-of select="invoice/@date"/></p>
<p>Customer: <xsl:value-of select="invoice/customer"/></p>
<p>Total: INR <xsl:value-of select="invoice/total"/></p>
</body></html>
</xsl:template>
Output
Invoice INV-2024-001 Date: 2024-03-15 Customer: Meena Sundar Total: INR 2900
Key Points to Remember
- xsl:value-of extracts the text value of a node specified by the select XPath expression.
- Use @attributeName to select attribute values.
- Use "." to reference the current node's text content.
- XPath functions like concat(), count(), and string-length() work inside the select attribute.
- disable-output-escaping="yes" outputs raw HTML stored in XML without escaping.
- xsl:copy-of copies the entire node including child elements; xsl:value-of copies only text.
Using xsl:value-of to Output Computed Values
xsl:value-of works with any XPath expression that evaluates to a string or number. You can embed calculations, function calls, and complex path expressions directly in the select attribute.
Computing a Total Price
<xsl:template match="cartItem">
<p>
<xsl:value-of select="productName"/> x
<xsl:value-of select="quantity"/> =
INR <xsl:value-of select="unitPrice * quantity"/>
</p>
</xsl:template>
Displaying a Formatted Date Component
<!-- Extract year from a date like "2024-03-15" --> <xsl:value-of select="substring(orderDate, 1, 4)"/>
Counting Child Elements
<p>This order contains <xsl:value-of select="count(items/item)"/> item(s). </p>
Displaying Concatenated Names
<xsl:value-of select="concat(firstName, ' ', lastName, ' (', department, ')')"/>
<!-- Output: Priya Mehta (Engineering) -->
Best Practices for xsl:value-of
- Always verify the selected node exists before using xsl:value-of — an empty node-set outputs an empty string without errors.
- Use normalize-space() when displaying user-entered data that may contain extra spaces.
- For numeric display, consider formatting with format-number() in XSLT 1.0: format-number(price, '#,##0.00') produces "1,299.00".
- Avoid using xsl:value-of on elements that contain child elements — it concatenates all descendant text, which may produce unexpected output.
