Friday, April 27, 2012

Quick look at XSLT (XSL Transformations)

What is XSLT?
XSL stands for EXtensible Stylesheet Language, and is a style sheet language for XML documents.
The XSLT language was defined by the World Wide Web Consortium (W3C), and version 1.0 of the language was published as a Recommendation on November 16, 1999.

How to transform XML into XHTML using XSLT.

Save the following content in score.xsl.

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <h2>Score card</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th>Player</th>
      <th>Score</th>
    </tr>
    <xsl:for-each select="card/cd">
    <tr>
      <td><xsl:value-of select="player"/></td>
      <td><xsl:value-of select="score"/></td>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>


Save the following content in score.xml


<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="score.xsl"?>
<!-- Edited by XMLSpy® -->
<card>
<cd>
<player>Sanath</player>
<score>400</score>
<country>Sri Lanka</country>
</cd>
<cd>
<player>Sachin</player>
<score>100</score>
<country>India</country>
</cd>
<cd>
<player>Afriddi</player>
<score>400</score>
<country>Pakistan</country>
</cd>
</card>


When you run it in browser you will get the following output.



Score card

Player Score
Sanath 400
Sachin 100
Afriddi 400

Some important links

What kind of language is XSLT?
http://www.ibm.com/developerworks/library/x-xslt/

XSLT tutorial
http://www.w3schools.com/xsl/default.asp

No comments:

Post a Comment