XSLT » Elements » xsl:value-of

Syntax:
<xsl:value-of
  select="expression"
  disable-output-escaping="yes" | "no"
/>

The xsl:value-of element is used to write or display in the result tree a string representation of the value assigned to a specified node. To explain it in another way, this XSLT element causes the value assigned to an XML tag to be displayed as text in the HTML page that we create to display the information in our XML file. There is no limit to the number of xsl:value-of elements that can appear in the code. However, each xsl:value-of element can only have one node assigned to it and this is accomplished by using the mandatory select attribute. Remember that a node can occur an unlimited number of times in an XML document. (For example, the <phone> ... </phone> xml tags in our DevGuru Staff List can be used for each staff member.)

You can use the xsl:for-each element to access each occurrence of a specified node in the order in which they occur in the document. As each occurrence is accessed, you can use the xsl:value-of element to display the value of the current node. As an example, you can use the xsl:for-each element to access all of the phone numbers in the <phone> ... </phone> XML tags in our DevGuru Staff List and then display the numbers using the xsl:value-of element. Additionally, you can use the xsl:sort element to define a sorting key which can be used to sort values.
 
You can also use the xsl:copy, xsl:copy-of, and xsl:text elements to display values as a string or text.
 
If the value is not a string, then the value will be converted to a string. (This is done automatically by calling the String function.) If the value is a Boolean, then the string will display as "true" or "false". If the value is a node-set, then only the first value of the first node in the set is displayed and the values of all other nodes in the set are completely ignored.
 
This is a self-closing element and it cannot contain any child elements or any content.

Examples

Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template>
<html>
<body>
<xsl:for-each select="devguru_staff/programmer">
    <div>
        NAME: <xsl:value-of select="name" />
        <br />
        DOB: <xsl:value-of select="dob" />
        <br />
        AGE: <xsl:value-of select="age" />
        <br />
        ADDRESS: <xsl:value-of select="address" />
        <br />
        PHONE: <xsl:value-of select="phone" />
        <hr />
    </div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
NAME: Bugs Bunny
DOB: 03/21/1970
AGE: 31
ADDRESS: 4895 Wabbit Hole Road
PHONE: 865-111-1111
NAME: Daisy Duck
DOB: 08/09/1949
AGE: 51
ADDRESS: 748 Golden Pond
PHONE: 865-222-2222
NAME: Minnie Mouse
DOB: 04/13/1977
AGE: 24
ADDRESS: 4064 Cheese Factory Blvd
PHONE: 865-333-3333
Explanation:

This is the code for xslt_example_valueof.xsl. In this example, we use the xsl:for-each and xsl:value-of elements to display all of the values assigned to the name, dob, age, address, and phone tags (nodes). The output has been truncated.
 
We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_valueof.xsl"?>

Language(s): XSLT

See Also: