Home arrow XML arrow Page 4 - Understanding XSLT Transformations: Matching and Selecting with Templates
XML

Understanding XSLT Transformations: Matching and Selecting with Templates


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.

Author Info:
By: Jagadish Chaterjee
Rating: 4 stars4 stars4 stars4 stars4 stars / 7
January 04, 2006
TABLE OF CONTENTS:
  1. · Understanding XSLT Transformations: Matching and Selecting with Templates
  2. · Matching with the particular node and selecting with "current context"
  3. · Matching with “root”, “particular node” and selecting in different ways
  4. · What are the default templates?

print this article
SEARCH DEVARTICLES

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.

blog comments powered by Disqus
XML ARTICLES

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials