Web Technology - Old Questions
9. Why do we need XSLT? Explain XSL <xls:for-each> Element.
XSLT (Extensible Stylesheet Language Transformations) provides the ability to transform XML data from one format to another automatically. It is the recommended style sheet language for XML.
XSLT is far more sophisticated than CSS. With XSLT we can add/remove elements and attributes to or from the output file. We can also rearrange and sort elements, perform tests and make decisions about which elements to hide and display, and a lot more.
XSLT uses XPath to find information in an XML document.
<xls:for-each> Element
- The <xsl:for-each> element allows you to do looping in XSLT.
- The XSL <xsl:for-each> element can be used to select every XML element of a specified node-set.
- The XSLT <xsl:for-each> element is used to apply a template repeatedly for each node.
syntax:
<xsl:for-each
select = Expression >
</xsl:for-each>
E.g.
<?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>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>