XSLT - Using <xsl:value-of> for Extracting and Converting Values
The <xsl:value-of> instruction in XSLT is used to select a value from the source XML document and place its text representation into the output document. It is one of the most commonly used instructions for displaying XML data in an HTML, XML, or text output. The value to be extracted is specified using the select attribute, which normally contains an XPath expression.
Basic Syntax
The general syntax of <xsl:value-of> is:
<xsl:value-of select="XPath-expression"/>
For example, consider the following XML document:
<student>
<name>Rahul</name>
<age>20</age>
<course>Computer Science</course>
</student>
An XSLT stylesheet can extract the student's name as follows:
<xsl:template match="/">
<html>
<body>
<h2>Student Details</h2>
<p>Name: <xsl:value-of select="student/name"/></p>
<p>Age: <xsl:value-of select="student/age"/></p>
<p>Course: <xsl:value-of select="student/course"/></p>
</body>
</html>
</xsl:template>
The <xsl:value-of> instruction evaluates each XPath expression and inserts the resulting value into the output. The resulting HTML would contain the values Rahul, 20, and Computer Science.
Extracting Values from Elements
The most basic use of <xsl:value-of> is extracting the text content of an XML element.
Suppose the source XML is:
<book>
<title>Learning XML</title>
<author>John Smith</author>
<price>450</price>
</book>
The following XSLT instructions extract individual values:
<xsl:value-of select="book/title"/>
<xsl:value-of select="book/author"/>
<xsl:value-of select="book/price"/>
The XPath expression tells XSLT which node should be selected. When the selected node is an element, <xsl:value-of> obtains its string value, which generally means its text content.
Extracting Attribute Values
<xsl:value-of> can also be used to retrieve XML attribute values. The @ symbol is used in XPath to identify an attribute.
For example:
<student id="S101">
<name>Anita</name>
<course>Mathematics</course>
</student>
The following instruction extracts the id attribute:
<xsl:value-of select="student/@id"/>
It produces:
S101
Similarly, an attribute can be combined with an element path:
<xsl:value-of select="student/name"/>
This extracts the student's name.
Using XPath Expressions
The select attribute does not have to contain only a simple element name. It can contain XPath expressions that perform calculations or manipulate values.
For example:
<product>
<price>500</price>
<quantity>3</quantity>
</product>
The total price can be calculated using:
<xsl:value-of select="product/price * product/quantity"/>
The result is:
1500
This demonstrates an important feature of <xsl:value-of>: it can output not only existing XML text but also the result of an XPath expression.
Converting Values
XPath expressions can work with different data types, including strings, numbers, and Boolean values. <xsl:value-of> converts the result of its select expression into a string for output.
For example:
<student>
<marks>85</marks>
</student>
An XSLT instruction such as:
<xsl:value-of select="student/marks + 5"/>
produces:
90
Here, the value "85" from the XML is used in a numeric expression. XPath performs the required conversion before calculating the result, and <xsl:value-of> outputs the final value as text.
Explicit conversion can also be performed with XPath functions such as:
<xsl:value-of select="number(student/marks)"/>
This converts the selected value into a numeric value before it is output.
For string conversion, the string() function can be used:
<xsl:value-of select="string(student/name)"/>
Boolean conversion can similarly be performed using:
<xsl:value-of select="boolean(student/name)"/>
Extracting Multiple Values
When an XPath expression selects multiple nodes, the behavior depends on the XSLT version and the expression being used. In common XSLT usage, <xsl:value-of> is intended to produce a single string value rather than independently processing every selected node.
For example:
<students>
<student><name>Rahul</name></student>
<student><name>Priya</name></student>
<student><name>Arun</name></student>
</students>
A stylesheet might use:
<xsl:value-of select="students/student/name"/>
In XSLT 1.0, when the expression selects multiple nodes, the string value of the first node in document order is returned. Therefore, this may produce:
Rahul
If the intention is to output every student's name, <xsl:for-each> or template processing is more appropriate:
<xsl:for-each select="students/student">
<p>
<xsl:value-of select="name"/>
</p>
</xsl:for-each>
This produces each student's name separately.
Difference Between <xsl:value-of> and <xsl:copy-of>
An important distinction is that <xsl:value-of> outputs the string value of the selected expression, whereas <xsl:copy-of> copies the selected node and its structure.
Consider:
<student>
<name>Rahul</name>
</student>
Using:
<xsl:value-of select="student/name"/>
produces:
Rahul
Using:
<xsl:copy-of select="student/name"/>
can produce:
<name>Rahul</name>
Therefore, <xsl:value-of> is useful when only the value is required, while <xsl:copy-of> is appropriate when the XML node itself needs to be copied.
Using Separators
In modern XSLT, <xsl:value-of> can use the separator attribute when the selected expression produces a sequence of values.
For example:
<xsl:value-of select="students/student/name" separator=", "/>
This can produce:
Rahul, Priya, Arun
The separator is particularly useful when multiple values need to be represented as a single textual result.
This feature is associated with later XSLT versions such as XSLT 2.0 and XSLT 3.0; it should not be confused with the behavior of <xsl:value-of> in XSLT 1.0.
Handling Missing Values
If the XPath expression does not select an existing value, <xsl:value-of> generally produces an empty string.
For example:
<student>
<name>Rahul</name>
</student>
If the stylesheet contains:
<xsl:value-of select="student/age"/>
but the age element does not exist, the output is empty.
A conditional expression can be used when a default value is required. In XSLT 2.0 and later, for example:
<xsl:value-of select="if (student/age) then student/age else 'Not Available'"/>
This produces Not Available when the age element is absent.
Practical Example
Consider this XML:
<employees>
<employee id="E101">
<name>Ravi</name>
<department>Development</department>
<salary>45000</salary>
</employee>
<employee id="E102">
<name>Meena</name>
<department>Testing</department>
<salary>40000</salary>
</employee>
</employees>
An XSLT stylesheet can display employee information as follows:
<xsl:template match="/">
<html>
<body>
<h2>Employee Details</h2>
<xsl:for-each select="employees/employee">
<p>
ID:
<xsl:value-of select="@id"/>
<br/>
Name:
<xsl:value-of select="name"/>
<br/>
Department:
<xsl:value-of select="department"/>
<br/>
Salary:
<xsl:value-of select="salary"/>
</p>
</xsl:for-each>
</body>
</html>
</xsl:template>
Here, <xsl:for-each> determines which employee is being processed, while <xsl:value-of> extracts the required individual values. The @id expression retrieves an attribute, while name, department, and salary retrieve child elements.
Advantages of <xsl:value-of>
<xsl:value-of> provides a simple way to extract XML data and place it into the result. It works naturally with XPath expressions, supports calculations and conversions, and is useful when transforming XML into HTML, text, or another XML structure. It also allows developers to avoid unnecessary processing when only the textual value of a node is required.
Important Points to Remember
The select attribute is mandatory for <xsl:value-of>. The expression inside select is evaluated using XPath, and the result is converted to a string for output. Element values can be selected using paths such as student/name, while attributes are accessed using expressions such as student/@id. XPath expressions can also perform calculations and type conversions. When several nodes need to be processed individually, <xsl:for-each> or template processing is generally more suitable than relying on a single <xsl:value-of> instruction.
In summary, <xsl:value-of> is primarily an XSLT value-extraction and output instruction. It connects XPath selection with the final textual output, making it fundamental for displaying and transforming data stored in XML.