To extract the value of a selected node, the <xsl:value-of> element is used in XSLT.
The <xsl:value-of> Element:
The extracted value of an XML element is also added to the output stream of the transformation, by the <xsl:value-of> element.
Example:
Books.xml:
<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book category="Child"> <title lang="en">ABC</title> <author>Author Name</author> <year>2020</year> <price>100.00</price> </book> <book category="IT"> <title lang="en">XQuery Book</title> <author>Author 1</author> <author>Author 2</author> <year>2020</year> <price>300.00</price> </book> </bookstore> |
XSLT Code:
<?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> <h2>List of Books</h2> <table border="1"> <tr bgcolor="#9acd32"> <th style="text-align:left">Title</th> <th style="text-align:left">Price</th> </tr> <tr> <td><xsl:value-of select="bookstore/book/title"/></td> <td><xsl:value-of select="bookstore/book/price"/></td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet> |
Output:
Explanation:
In the above example, an XPath expression is present in the select attribute, that works like navigating a file system, where a forward slash (/) selects subdirectories.
Please Share