XSLT <value-of> Element

XSLT <value-of> Element is used to extract value from a node selected through name or expression. It is used to get value from XML and then display or output it. In this tutorial, we will learn to implement it.

XSLT is a programming language used to convert XML documents into other forms such as HTML, PDF, JPEG, PNG, etc.

Syntax

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

Attributes

  • select (expression): It is a required field and we need to provide the path or node to select and extract the data from. “/” is used to select data from subdirectories.
  • disable-output-escaping (yes/no): It is an optional parameter with a default value as yes. If yes, expressions like “<” will be displayed as it is. Otherwise, the expression would be displayed as “&lt;”.

Let us implement it in the following examples:

Example 1: Selecting from a single node.

XML




<!--Tutorials.xml-->
  
<?xml version="1.0" encoding="UTF-8"?>
<tutorials>
  <programming>
    <title>Java</title>
    <description>Java is a programming language</description>
  </programming>
</tutorials>


XML




<!-- index.xslt-->
  
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
<xsl:template match="/">
  <html>
  <body>
    <h1>Welcome to w3wiki</h1>
    <h3>XSLT value-of Element</h3>
  
    <b><p><xsl:value-of select="tutorials/programming/title"/></p></b>
    <p><xsl:value-of select="tutorials/programming/description"/></p>
  </body>
  </html>
</xsl:template>
  
</xsl:stylesheet>


Output:

Example 2: Displaying a list using value of.

XML




<!--Tutorials.xml-->
  
<?xml version="1.0" encoding="UTF-8"?>
<tutorials>
  <programming>
    <title>Java</title>
    <description>Java is a programming language</description>
  </programming>
  <programming>
    <title>Python</title>
    <description>Python is a programming language</description>
  </programming>
  <programming>
    <title>PHP</title>
    <description>PHP is a programming language</description>
  </programming>
  <programming>
    <title>JavaScript</title>
    <description>JavaScript is a programming language</description>
  </programming>
  <programming>
    <title>C++</title>
    <description>C++ is a programming language</description>
  </programming>
</tutorials>


XML




<!--index.xslt-->
  
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
<xsl:template match="/">
  <html>
  <body>
    <h1>Welcome to w3wiki</h1>
    <h3>XSLT value-of Element</h3>
  
    <ul>
    <xsl:for-each select="tutorials/programming">
    <li><b><xsl:value-of select="title"/></b>: <xsl:value-of select="description"/></li>
  
    </xsl:for-each>
    </ul>
  </body>
  </html>
</xsl:template>
  
</xsl:stylesheet>


Output: