ASP.NET
  Home arrow ASP.NET arrow Page 3 - Working with xsl:choose in XSLT
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
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? 
ASP.NET

Working with xsl:choose in XSLT
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2006-12-11

    Table of Contents:
  • Working with xsl:choose in XSLT
  • Working with xsl:choose in XSLT: introduction
  • Working with xsl:choose in XSLT: explanation
  • Emitting HTML together with CSS using XSLT
  • Working with multiple xsl:when within xsl:choose in XSLT
  • More conditions within xsl:when

  • 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


    Working with xsl:choose in XSLT - Working with xsl:choose in XSLT: explanation


    (Page 3 of 6 )

    Let us understand the code step by step.  Let us start with the following:

    <xsl:template match="/">

          <table border="1">

          <xsl:apply-templates select="SQLData/Rows" />

          </table>

    </xsl:template>

    From the above code fragment, you can easily understand that I am emitting a TABLE tag and the content of the TABLE tag is based on the template defined in each and every “SQLData/Rows.”  The template for “SQLData/Rows” is defined as follows:

    <xsl:template match="SQLData/Rows">

          <tr>

                <td>

                      <xsl:value-of select="Empno"/>

                </td>

                <td>

                      <xsl:value-of select="Ename"/>

                </td>

                <td>

                      <xsl:apply-templates select="Sal" />

                </td>

                <td>

                      <xsl:value-of select="Deptno"/>

                </td>

          </tr>

    </xsl:template>

    According to the above, you can understand that I am emitting a TR tag for every “SQLData/Rows.”  Within the TR, I am also emitting a TD tag along with the content available in leaf nodes (like Empno, Ename etc.).  The only exception is the following TD emission:

                <td>

                      <xsl:apply-templates select="Sal" />

                </td>

    The above emits a TD tag but the content is based on the template defined separately for the “Sal” node.  The template for the “Sal” node is defined as follows:

    <xsl:template match="Sal">

          <xsl:choose>

                <xsl:when test="text() > '3500'">

                      <b>

                            <xsl:value-of select="."/>

                      </b>

                </xsl:when>

                <xsl:otherwise>

                      <xsl:value-of select="."/>

                </xsl:otherwise>

          </xsl:choose>

    </xsl:template>

    The above template may be necessary as we need to test for a condition on “Sal.” From the above code fragment, you can understand that a single “xsl:choose” can be embedded with any number of conditions using “xsl:when.” The “text()” is nothing but the content of “Sal” (in this case). 

    The text gets compared with a value of 3500 and emits the text with bold tags if the condition evaluates to true.  If the condition evaluates to false, “xsl:otherwise” gets executed and it emits only the content (without any bold tags).

    More ASP.NET Articles
    More By Jagadish Chaterjee


     

    ASP.NET ARTICLES

    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ
    - Developing a Dice Game Using ASP.NET Futures...
    - Completing an ASP.NET AJAX Server-Centric Ba...
    - Information Management for an ASP.NET AJAX S...
    - Comment and Order Management for an ASP.NET ...
    - Back-end Management Tasks for an ASP.NET AJA...
    - User Information Management for an ASP.NET A...
    - Adding Comments and Search to an ASP.NET AJA...





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