ASP.NET
  Home arrow ASP.NET arrow Page 5 - Dealing with Attributes and Elements in XP...
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

Dealing with Attributes and Elements in XPath with XSLT using ASP.NET 2.0
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 7
    2007-01-09

    Table of Contents:
  • Dealing with Attributes and Elements in XPath with XSLT using ASP.NET 2.0
  • Retrieving all values of a particular attribute at any level of hierarchy (or path)
  • Retrieving all values of all attributes available at any level of hierarchy (or path)
  • Retrieving values of attributes and elements simultaneously
  • Displaying an HTML table based on the hierarchy
  • Searching for an element at a particular location

  • 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


    Dealing with Attributes and Elements in XPath with XSLT using ASP.NET 2.0 - Displaying an HTML table based on the hierarchy


    (Page 5 of 6 )

    Now, let us proceed to generate an HTML table based on the XML document we have.  The following is the first approach (using separate templates) we can use to accomplish the same:

    <?xml version="1.0" encoding="utf-8"?>

    <xsl:stylesheet version="1.0"

        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

          <xsl:template match="/">

                <table border="1">

                      <tr>

                            <xsl:apply-templates select ="EmployeeDetails/Department"/>      

                      </tr>

                </table>

          </xsl:template>

          <xsl:template match="EmployeeDetails/Department">

                <td colspan="3">

                      <b>

                            Department: <xsl:value-of select ="@Dname"/>

                      </b>

                </td>

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

          </xsl:template>

          <xsl:template match ="Employee"     >

                <tr>

                      <td>

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

                      </td>

                      <td>

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

                      </td>

                      <td>

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

                      </td>

                </tr>

          </xsl:template>

         

    </xsl:stylesheet>

    The total skill lies in identifying and placing the tags appropriately at their respective positions in the templates. The following is the second approach we can use to accomplish the same task (using a nested “xsl:for-each” construct):

    <?xml version="1.0" encoding="utf-8"?>

    <xsl:stylesheet version="1.0"

        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

          <xsl:template match="/">

                <table border="1">

                      <xsl:for-each select="EmployeeDetails/Department">

                            <tr>

                                  <td colspan="3">

                                        <b>

                                              Department: <xsl:value-of select ="@Dname"/>

                                        </b>

                                  </td>

                            </tr>

                            <xsl:for-each select="Employee">

                                  <tr>

                                        <td>

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

                                        </td>

                                        <td>

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

                                        </td>

                                        <td>

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

                                        </td>

                                  </tr>

                            </xsl:for-each>

                      </xsl:for-each>

                </table>

          </xsl:template>

    </xsl:stylesheet>

    In any of the above cases, when the above XSLT gets executed, the transformation should look something like the following:

    <table border="1">

          <tr>

                <td colspan="3">

                      <b>Department: Accounting</b>

                </td>

          </tr>

          <tr>

                <td>1001</td>

                <td>Jagadish</td>

                <td>3400</td>

          </tr>

          <tr>

                <td>1002</td>

                <td>Chatarji</td>

                <td>2500</td>

          </tr>

          <tr>

                <td>1003</td>

                <td>Winner</td>

                <td>4300</td>

          </tr>

    .

    .

    .

    </table>

    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 4 hosted by Hostway