XML
  Home arrow XML arrow Page 4 - 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  
Mobile Linux 
App Generation ROI 
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 - What are the default templates?


    (Page 4 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_6.xsl).  Now, let us look at “Sample2_6.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="Name">
                     <tr>
                       <td>
                         <xsl:value-of select="."/>
                       </td>
                     </tr>
                </xsl:template>
    </xsl:stylesheet>

    Let us look at only the matches and selections from the above XSL.

    <xsl:template match="/">
          <xsl:apply-templates select="Names"/>
    <xsl:template match="Name">
          <xsl:value-of select="."/>

    The first statement starts at the “root”.  The second tries to apply any templates (if available) to all the elements available in “Names” (including itself).  The third statement is a template, which accepts only “Name” element (which may be available again in the same path of “Names”).  The fourth statement just displays the text available in “Name” element (along with all of its nested elements).

    From the above paragraph, we can understand that the text available in every element of “Name” will be in separate “td”.  But, what about the text for “Names”?  No template exists for “Names” element.  And then the “default template” comes into the scenario (which is hidden inside the parser).  According to this “default template”, any element not matched with any template, the text would be directly carried out without any transformation.

    Before execution, make sure that you modify the XSL file name (to “Sample2_6.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">
                these are the names
                <tr><td>Jag</td></tr>
                <tr><td>Win</td></tr>
                <tr><td>Dhan</td></tr>
                <tr><td>Ram</td></tr>
    </table>
                </body>
    </html>

    From the above you can understand that the text in “Names” is directly carried out and the text in every “Name” element is enclosed within “TR” and “TD”.

    A final touch!

    If you are experiencing troubles, try to be always “specific”.  Always work with proper “matching”, “selecting” and “templating”.  For example, the following XSL (Sample2_7.xsl) may be reasonable for your criteria (but it is up to you).

    <?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/Name"/>
                         </table>
                       </body>
                     </html>
                </xsl:template>
                <xsl:template match="Name">
                     <tr>
                       <td>
                         <xsl:value-of select="."/>
                       </td>
                     </tr>
                </xsl:template>
    </xsl:stylesheet>

    The above excludes the text of “Names” element.

    Any comments, suggestions, ideas, improvements, bugs, errors, feedback etc. are highly appreciated at jag_chat@yahoo.com.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 1 hosted by Hostway
    Stay green...Green IT