XSLT `xsl:value-of` Element: Extracting and Inserting XML Node Values

Learn how to use the XSLT `xsl:value-of` element to extract and insert data from XML documents into your transformed output. This tutorial explains its key attributes (`select`, `disable-output-escaping`), providing practical examples for creating dynamic and data-driven transformations.



Using the XSLT `xsl:value-of` Element

The XSLT `xsl:value-of` element is used to insert the value of a selected node into the output. It's a fundamental element in XSLT for extracting data from an XML document and incorporating that data into the transformed output. The value is inserted as text.

`xsl:value-of` Element Syntax and Attributes


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

The key attributes are:

  • select: Specifies the XPath expression to evaluate. This expression determines which node's value should be selected and inserted into the output.
  • disable-output-escaping: A boolean value (`yes` or `no`, default is `no`). If set to `yes`, special XML characters (like `<`, `>`, `&`) in the selected node's value are not escaped; they are treated as literal characters.

Example: Extracting Employee Details

(Note: This example requires an XML file named `employee.xml` and an XSLT stylesheet named `employee.xsl`. Screenshots from the original text are not included here. Please refer to the original document for visual verification of the example and XSLT output. The descriptions below aim to convey the information present in those screenshots.)

This example uses `xsl:value-of` to extract employee details from an XML file and display them in an HTML table. The structure of the XML file and the XSLT stylesheet would be defined here in a real HTML file.

`employee.xml` (Example XML Data)


<employees>
  <employee>
    <firstName>Aryan</firstName>
    <lastName>Gupta</lastName>
    <nickName>Raju</nickName>
    <salary>30000</salary>
  </employee>
  <!-- ... more employee elements ... -->
</employees>

`employee.xsl` (XSLT Stylesheet)

(The original content includes the content of the `employee.xsl` file. Since we cannot display the file here, please refer to the original document for the file content.)

Test it Now

(The original content includes a screenshot showing the output. Since we cannot display images, please refer to the original document for the visual representation of the output.)

The output shows an HTML table where the `xsl:value-of` elements extract the values from the employee elements in the XML file and places them in the appropriate table cells.