Docstoc

XSLT

Document Sample
XSLT Powered By Docstoc
					                                    XSLT
 XSL stands for EXtensible Stylesheet Language, and is a style sheet language for XML
 documents.
 XSLT stands for XSL Transformations.


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




      XSLT = XSL Transformations
                      XSLT - Transformation
How to transform XML into XHTML using XSLT
1. Create an xml file first
2. Create an XSL Style Sheet
3. Link the XSL Style Sheet to the XML Document

 Note
  Correct Style Sheet Declaration
 Ex
 <?xml version="1.0" encoding="ISO-8859-1"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  Linking the XSL Style Sheet to the XML Document
 Ex
 <?xml version="1.0" encoding="ISO-8859-1"?>
 <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
                 Important XSLT Elements
1. <xsl:template> Element

   The <xsl:template> element is used to build templates and the match
   attribute is used to associate a template with an XML element.


                 Ex

                 <xsl:template match="/">
                  -----
                  ------
                  ____
                 </xsl:template>
2. <xsl:value-of> Element

   The <xsl:value-of> element can be used to extract the value of an XML element
   and add it to the output stream of the transformation:
        Ex
        <xsl:template match="/">
         <html>
         <body>
         <h2>Marksheet of Students-NVQ-5</h2>
         <table border="1">
          <tr bgcolor="#9acd32">
             <th>Index-No</th>
             <th>Name</th>
             <th>Marks</th>
          </tr>
         <tr>
           <td> <xsl:value-of select="marksheet/record/indexno"/> </td>
           <td> <xsl:value-of select=“marksheet/record/name"/> </td>
           <td> <xsl:value-of select=“marksheet/record/marks"/> </td>
          </tr>
        </table>
         </body>
         </html>
        </xsl:template>
3. <xsl:for-each> Element
   The XSL <xsl:for-each> element can be used to select every XML element of a specified
   node-set
EX
<xsl:template match="/">
 <html>                                    Note
 <body>                                    The output can also be filtered from the XML file by adding a
 <h2>Marksheet of Students-NVQ-5</h2> criterion to the select attribute in the <xsl:for-each>
 <table border="1">                        element.
  <tr bgcolor="#9acd32">                   <xsl:for-each select=“marksheet/record[marks &gt;’70’]">
   <th>Index-No</th>
   <th>Name</th>                           Legal filter operators
   <th>Marks</th>                          = (equal)              &gt; greater than
  </tr>                                    != (not equal)         &lt; less than
<xsl:for-each select="marksheet/record">
<xsl:sort select="marks"/>
<tr>
   <td><xsl:value-of select="indexno"/></td>
   <td><xsl:value-of select="name"/></td>
   <td><xsl:value-of select="marks"/></td>
  </tr>
</xsl:for-each>
</table>
 </body>
 </html>
</xsl:template>
4. <xsl:sort> Element

     The <xsl:sort> element is used to sort the output.
Ex

<xsl:template match="/">
          ------
          -------
          -------
<xsl:for-each select="marksheet/record">
<xsl:sort select="marks"/>
<tr>
   <td><xsl:value-of select="indexno"/></td>
   <td><xsl:value-of select="name"/></td>
   <td><xsl:value-of select="marks"/></td>
  </tr>
</xsl:for-each>
          -----
          -------
          -----
</xsl:template>
5. <xsl:if> Element

  The <xsl:if> element is used to put a conditional test against the content of the XML file.

    Syntax
    <xsl:if test="expression">
     ...some output if the expression is true...
    </xsl:if>
               Ex
               <xsl:template match="/">
                          ------
                          -------
               <xsl:for-each select="marksheet/record">
               <xsl:if test="marks &gt; 70"/>
               <tr>
                  <td><xsl:value-of select="indexno"/></td>
                  <td><xsl:value-of select="name"/></td>
                  <td><xsl:value-of select="marks"/></td>
                 </tr>
               <xsl:if />
               </xsl:for-each>
                          -----
                          -------
               </xsl:template>
Complete Example

  <?xml version="1.0" encoding="ISO-8859-1"?>
  <?xml-stylesheet type="text/xsl" href="marksheet.xsl"?>
  <marksheet>

   <record>
      <indexno>123</indexno>
           <name>Lakmal</name>
           <marks>80</marks>
   </record>
   <record>
                                                            XML
           <indexno>124</indexno>
           <name>Nishanthi</name>                           File
           <marks>85</marks>
   </record>
   <record>
           <indexno>125</indexno>
           <name>Pasindu</name>
           <marks>68</marks>
   </record>

  </marksheet>
<?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>Marksheet of Students-NVQ-5</h2>
 <table border="1">
  <tr bgcolor="#9acd32">
   <th>Index-No</th>
   <th>Name</th>
   <th>Marks</th>
  </tr>                                                                               XSL
<xsl:for-each select="marksheet/record">
<xsl:sort select="marks"/>                                                        Style Sheet
<tr>
   <td><xsl:value-of select="indexno"/></td>
   <td><xsl:value-of select="name"/></td>
   <td><xsl:value-of select="marks"/></td>
  </tr>
</xsl:for-each>
</table>
 </body>
 </html>
</xsl:template>
</xsl:stylesheet>

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:10
posted:4/1/2011
language:English
pages:9