Friday, March 2, 2012

Understanding XSLT

XSLT(eXtensible style sheet language transformations) is a language to transform a XML document into XHTML document or other XML document.

CSS = Style sheet for HTML document
XSL = Style sheet for XML document

When we open XML document in any browser it opens as it is in text form. Because XML has user defined tags (not predefined) and browser don't know how to display that.
We can create XSLT for any XML document so with the help of XSLT browser understand how to display given XML.

* XSLT stands for XSL Transformations
* XSLT is the most important part of XSL
* XSLT transforms an XML document into another XML document
* XSLT uses XPath to navigate in XML documents
* XSLT is a W3C Recommendation



XPath is used to navigate in XML document. It searches and finds nodes, elements, attributes to perform some operations on them.(Learn in detail http://www.w3schools.com/xpath/default.asp)(Read other post "Understanding XPath").

* XSLT - a language for transforming XML documents
* XPath - a language for navigating in XML documents
* XSL-FO - a language for formatting XML documents

Example 

XML document

<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="cdcatelog.xsl"?> <!-- Calling XSLT doc given below-->
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Empire dosan</title>
<artist>Peter Cristian</artist>
<country>Australia</country>
<company>JDAG</company>
<price>100.15</price>
<year>1990</year>
</cd>
</catalog>

XSLT Document
<?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>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>
</xsl:stylesheet>


Output :



My CD Collection

Title Artist
Empire Burlesque Bob Dylan
Empire dosan Peter Cristian





No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...