<xsl:value-of>

XSLT Function: <xsl:select>The XSLT <xsl:value-of> element is used to extract a value from an expression defined in the select attribute.

The expression defined in the mandatory select attribute is either an XPATH expression (for nodes and/or values) or a variable reference.

The return of the <xsl:value-of> function is a literal value.

 


Syntax


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

where expression is either an XPATH expression or an XSLT variable reference.

Attributes

namemandatory/optionaldescription
selectMANDATORYan XPATH expression or variable reference.
disable-output-escapingOPTIONALdetermines if
escaped characters in the output are converted to normal
characters

Examples

For our examples we are using the fictional library as an input XML. Any XSL transform examples we define uses this XML as an input.

<?xml version="1.0" encoding="UTF-8"?>
<library>
<category name="dogs">
<book>
<name>All about dogs</name>
<author>Someone</author>
<isin>true</isin>
<daysuntilreturn>0</daysuntilreturn>
</book>
</category>
<category name="cats">
<book>
<name>All about cats</name>
<author>Someone</author>
<isin>false</isin>
<daysuntilreturn>3</daysuntilreturn>
</book>
</category>
</library>

 

How to select a node value

The following XSL transform shows how to retrieve an arbitrary node (element or attribute) value from a source document and write it to a target document.

This example matches each category node, and then uses the <xsl:value-of> function to retrieve and print the category name.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="/*/category"/>
</xsl:template>
<xsl:template match="category">
Found a category: <xsl:value-of select="./@name" />
</xsl:template>
</xsl:stylesheet>

The output from the above XSL transform is as follows:

Found a category: dogs
Found a category: cats

 

How to select an XPATH expression value

The following XSL transform shows how to use an XPATH expression to generate a value and then write the result as an output to a target document.

This example uses XPATH to count of each category node in the source document and print the result.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
Number of categories: <xsl:value-of select="count(//category)" />
</xsl:template>
</xsl:stylesheet>

The output from the above XSL transform is as follows:

Number of categories: 2

How to select a variable value

The following XSL transform shows how to query a variable and write its value as an output to a target document.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="my_constant" select="'a constant value'" />
<xsl:variable name="some_value" select="/*/category[@name='dogs']/book[1]/name" />
<xsl:template match="/">
Variable (Constant): <xsl:value-of select="$my_constant" />
Variable (Select): <xsl:value-of select="$some_value" />
</xsl:template>
</xsl:stylesheet>

The output from the above XSL transform is as follows:

Variable (Constant): a constant value
Variable (Select): all about dogs

Posted in XSLT and tagged .

Leave a Reply