XML
  Home arrow XML arrow 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 
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


    (Page 1 of 4 )

    This article mainly concentrates on the basics of “matching” and “selecting” different areas of XML and transforming those using different templates. This is the second part of the "Understanding XSLT Transformations" series. If you are new to the series, I strongly encourage you to go through the first article of this series.
    A downloadable file for this article is available here.

    You can test all of the examples of this article with the utility I enclosed in my first article (developed using Visual Studio 2005).  Remember that the utility (or the “exe”) works only on computers where .NET Framework 2.0 is installed (.NET framework 2.0 is freely downloadable from the Microsoft website).

    Matching with “root” and selecting with “current context”

    This example mainly contains two files. The first would be an XML file (Sample2.xml) and the second would be an XSL file (Sample2_1.xsl).  Let us go through the “Sample2.xml” first.

    <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type="text/xsl" href="Sample2_6.xsl"?>
    <Names>
                these are the names
                <Name>Jag</Name>
                <Name>Win</Name>
                <Name>Dhan</Name>
                <Name>Ram</Name>
    </Names>

    I developed the above XML only for testing with several scenarios.  So, I ask that you not worry about the proper XML structure conformances. Now, let us look at “Sample2_1.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="."/>
              </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="."/>
                            …
                </xsl:template>

    The matching is based on “root node” which is “/”.  The selection is based on “.”.  The “dot” refers to the current context, which finds “Names” from the “root node”.  The “dot” retrieves all the text of the current node as well as all nested nodes of the same.  And thus you will have output something like the following:

    <?xml version="1.0" encoding="utf-16"?>
    <html>
                <body>
                            <b>
                these are the names
                Jag
                Win
                Dhan
                Ram
    </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

    - Using Regions with XSL Formatting Objects
    - Using XSL Formatting Objects
    - More Schematron Features
    - Schematron Patterns and Validation
    - Using Schematron
    - 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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek