XML
  Home arrow XML arrow Page 3 - Understanding XSLT Transformations: Matchi...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

Understanding XSLT Transformations: Matching and Selecting with Templates
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2006-01-04

    Table of Contents:
  • Understanding XSLT Transformations: Matching and Selecting with Templates
  • Matching with the particular node and selecting with "current context"
  • Matching with “root”, “particular node” and selecting in different ways
  • What are the default templates?

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Understanding XSLT Transformations: Matching and Selecting with Templates - Matching with “root”, “particular node” and selecting in different ways


    (Page 3 of 4 )

    This example mainly contains two files.  The first would be an XML file (Sample2.xml), which is same as above and the second would be an XSL file (Sample2_5.xsl).  Now, let us look at “Sample2_5.xsl”:

    <?xml version="1.0" encoding="utf-8"?>
     
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                <xsl:output method="html"/>
                <xsl:template match="/">
                         <html>
                           <body>
                             <table border="1">
                               <xsl:apply-templates select="Names"/>
                             </table>
                           </body>
                         </html>
                </xsl:template>
                <xsl:template match="Names">
                         <tr>
                           <td>
                             <xsl:value-of select="."/>
                           </td>
                         </tr>
                </xsl:template>
    </xsl:stylesheet>

    The above XSL is bit different from all of the previous ones.  Here I am using two types of templates.  One matches on “root” and the other on “Names”.  When we work with “root” template node, the flow of execution always starts at “root” template.  So, you can understand that, it starts emitting HTML, BODY and TABLE.

    After emitting “TABLE” tag the control searches for other templates based on matching of “Names” and other nested elements.  And automatically, it finds the second template “Names” (which is in the same line).  And finally displays the entire text available in “Names” (along with whole text of nested elements).

    Before execution, make sure that you modify the XSL file name (to “Sample2_5.xsl”) within the XML document.  When you execute the above you will get the following output, which is different from all of the above:

    <?xml version="1.0" encoding="utf-16"?>
    <html>
                <body>
                            <table border="1">
                                        <tr>
                                                    <td>
                these are the names
                Jag
                Win
                Dhan
                Ram
    </td>
                                        </tr>
                            </table>
                </body>
    </html>

    But the above is not the way we want.  We want every name to be displayed in a different “td”.  I shall explain to you about it later in the coming sections.

    Matching with “root” and selecting with “element path” – extended way

    This example mainly contains two files.  The first would be an XML file (Sample2.xml), which is same as above and the second would be an XSL file (Sample2_4.xsl).  Now, let us look at “Sample2_4.xsl”:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                <xsl:output method="html"/>
                <xsl:template match="/">
                    <html>
                      <body>
                        <b>
                          <xsl:value-of select="Names/Name"/>
                        </b>
                      </body>
                    </html>
                </xsl:template>
    </xsl:stylesheet>

    The most important statements of the whole XSL are the following:

                <xsl:template match="/">
                            …
                            <xsl:value-of select="Names/Name"/>
                            …
                </xsl:template>

    Now, the matching is again based on “root” node.  The selection is based on “Names/Name” which is bit different than that of the previous one.  Within the root node, you are selecting only “Names/Name” element (only the first element).  It finally retrieves all the text of the element “Name” (along with its own nested elements) available in “Names” element from the “root” node. 

    Before execution, make sure that you modify the XSL file name (to “Sample2_4.xsl”) within the XML document.  When you execute the above you will get the following output, which is different from all of the above:

    <?xml version="1.0" encoding="utf-16"?>
    <html>
                <body>
                            <b>Jag</b>
                </body>
    </html>

    More XML Articles
    More By Jagadish Chaterjee


       · We hope you found this article educational and useful. Please feel free to let the...
       · Hello guys! Feel free to give any suggestions or comments or new requirements...
     

    XML ARTICLES

    - Datatypes and More in RELAX NG
    - Providing Options in RELAX NG
    - An Introduction to RELAX NG
    - Path, Predicates, and XQuery
    - Using Predicates with XQuery
    - Navigating Input Documents Using Paths
    - XML Basics
    - Introduction to XPath
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE
    - UI Design with Java and XML Toolkits
    - Displaying ADO Retrieved Data with XML Islan...
    - Widget Walkthrough
    - Introduction to Widgets
    - The Why and How of XML Data Islands







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway