Friday, October 7, 2011

Core XSTL


Use Can Also Learn XSTL by Using click on XSTL Tab.




XSLT Tutorial

  • XSL stands for EXtensible Stylesheet Language, and is a style sheet language for XML documents. XSLT stands for XSL Transformations. In this tutorial you will learn how to use XSLT to transform XML documents into other formats, like XHTML.
  • It started with XSL and ended up with XSLT, XPath, and XSL-FO.
  • The World Wide Web Consortium (W3C) started to develop XSL because there was a need for an XML-based Stylesheet Language.
  • XSL = Style Sheets for XML
  •  XSL consists of three parts:
    • XSLT - a language for transforming XML documents
    • XPath - a language for navigating in XML documents
    • XSL-FO - a language for formatting XML documents.

XSLT is a language for transforming XML documents into XHTML documents or to other XML documents.
XPath is a language for navigating in XML documents.

What is XSLT?
  • 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

How to transform XML into XHTML using XSLT ?
Declare:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
or
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

Example:
Step1: Make a Xsltdemo.xml file
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!-- Edited by XMLSpy® -->
<?xml-stylesheet type="text/xsl"  href="Xsltdemo.xsl"?>
<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>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>

</catalog>

Step2: Make a Xsltdemo.xsl file
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<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>
<th>Country</th>
<th>Company</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title" /></td>
        <td><xsl:value-of select="artist" /></td>
<td><xsl:value-of select="country" /></td>
<td><xsl:value-of select="company" /></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

XSLT Template: An XSL style sheet consists of one or more set of rules that are called templates.
A template contains rules to apply when a specified node is matched.

  • The <xsl:template> element is used to build templates.
  • The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document)
Tags that are used in XSLT:
  1. <xsl:template> : Syntax  <xsl:template match="/">
  2. <xsl:value-of>  : The <xsl:value-of> element is used to extract the value of a selected node.               <xsl:value-of select="catalog/cd/title"/>
  3. <xsl:for-each> :  The <xsl:for-each> element allows you to do looping in XSLT.                         <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>
  4. <xsl:sort>: The <xsl:sort> element is used to sort the output.                                                       <xsl:sort select="artist"/>
  5. <xsl:if> : The <xsl:if> element is used to put a conditional test against the content of the XML file. <xsl:if test="price &gt; 10">
            <tr>
              <td><xsl:value-of select="title"/></td>
              <td><xsl:value-of select="artist"/></td>
            </tr>
          </xsl:if>
  6. The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests.                                                                                                          <xsl:for-each select="catalog/cd">
        <tr>
          <td><xsl:value-of select="title"/></td>
          <xsl:choose>
            <xsl:when test="price &gt; 10">

              <td bgcolor="#ff00ff">
              <xsl:value-of select="artist"/></td>
            </xsl:when>
            <xsl:otherwise>

              <td><xsl:value-of select="artist"/></td>
            </xsl:otherwise>
          </xsl:choose>

        </tr>
        </xsl:for-each>
  7. The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes.
Examples:
  1. <?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>
      <xsl:apply-templates/>
      </body>
      </html>
    </xsl:template>

    <xsl:template match="cd">
      <p>
      <xsl:apply-templates select="title"/>
      <xsl:apply-templates select="artist"/>
      </p>
    </xsl:template>

    <xsl:template match="title">
      Title: <span style="color:#ff0000">
      <xsl:value-of select="."/></span>
      <br />
    </xsl:template>

    <xsl:template match="artist">
      Artist: <span style="color:#00ff00">
      <xsl:value-of select="."/></span>
      <br />
    </xsl:template>

    </xsl:stylesheet>
2. <?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>
    <tr>
      <td><xsl:value-of select="catalog/cd/title"/></td>
      <td><xsl:value-of select="catalog/cd/artist"/></td>
    </tr>

<xsl:for-each select="catalog/cd">
      <xsl:sort select="artist"/>
      <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
      </tr>
    </xsl:for-each>

<xsl:if test="price &gt; 10">
        <tr>
          <td><xsl:value-of select="title"/></td>
          <td><xsl:value-of select="artist"/></td>
        </tr>
      </xsl:if>

<xsl:for-each select="catalog/cd">
    <tr>
      <td><xsl:value-of select="title"/></td>
      <xsl:choose>
        <xsl:when test="price &gt; 10">

          <td bgcolor="#ff00ff">
          <xsl:value-of select="artist"/></td>
        </xsl:when>
        <xsl:otherwise>

          <td><xsl:value-of select="artist"/></td>
        </xsl:otherwise>
      </xsl:choose>

    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>




No comments:

Post a Comment