Library tutorials & articles

Using ADO.NET, XML & XSL to generate HTML

The XML

Here is the XML generated by ds.WriteXml:

<?xml version="1.0" standalone="yes"?>
<Menu>
  <MenuItem>
    <ItemText>Home</ItemText>
    <ItemLink>index.html</ItemLink>
    <ItemType>Item</ItemType>
  </MenuItem>
  <MenuItem>
    <ItemText>-</ItemText>
    <ItemLink>-</ItemLink>
    <ItemType>Space</ItemType>
  </MenuItem>
  <MenuItem>
    <ItemText>Bookstore</ItemText>
    <ItemLink>-</ItemLink>
    <ItemType>H2</ItemType>
  </MenuItem>
  <MenuItem>
    <ItemText>C#</ItemText>
    <ItemLink>csharp.html</ItemLink>
    <ItemType>Item</ItemType>
  </MenuItem>
  <MenuItem>
    <ItemText>.NET</ItemText>
    <ItemLink>dotnet.html</ItemLink>
    <ItemType>Item</ItemType>
  </MenuItem>
  <MenuItem>
    <ItemText>Notebooks</ItemText>
    <ItemLink>-</ItemLink>
    <ItemType>H2</ItemType>
  </MenuItem>
  <MenuItem>
    <ItemText>C#</ItemText>
    <ItemLink>notebooks/csharp/index.html</ItemLink>
    <ItemType>Item</ItemType>
  </MenuItem>
  <MenuItem>
    <ItemText>ATL</ItemText>
    <ItemLink>-</ItemLink>
    <ItemType>H3</ItemType>
  </MenuItem>
  <MenuItem>
    <ItemText>Hello World</ItemText>
    <ItemLink>hello.html</ItemLink>
    <ItemType>Item</ItemType>
  </MenuItem>
  <MenuItem>
    <ItemText>Handling Errors</ItemText>
    <ItemLink>errors.html</ItemLink>
    <ItemType>Item</ItemType>
  </MenuItem>
</Menu>

Here is the XSL used to transform the XML into HTML:


<xsl:stylesheet
 version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns="http://www.w3.org/TR/REC-html40">

<xsl:output method="html" indent="yes" />
<xsl:template match="/">
<html>
<body>
<table border="0" cellpadding="2" cellspacing="0" id="menu">
     <xsl:for-each select="Menu/MenuItem">
       <tr>
         <td height="20" nowrap="">
           <xsl:choose>
             <xsl:when test="ItemType='Item'">
               <a>
                 <xsl:attribute name="href">
                 <xsl:value-of select="ItemLink"/>
                 </xsl:attribute>
                 <font size="3">
                   <xsl:value-of select="ItemText"/>
                 </font>
               </a>
             </xsl:when>
             <xsl:when test="ItemType='H2'">
               <center>
                 <font size="4" color="#800000">
                   <b>
                     <xsl:value-of select="ItemText"/>
                   </b>
                 </font>  
               </center>
             </xsl:when>
             <xsl:when test="ItemType='H3'">
               <center>
                 <font size="3">
                   <b>
                     <xsl:value-of select="ItemText"/>
                   </b>
                 </font>  
               </center>
             </xsl:when>
           </xsl:choose>
         </td>
       </tr>
     </xsl:for-each>
</table>
</body>
</html>
</xsl:template>
 
</xsl:stylesheet>

Comments

  1. 11 Dec 2005 at 12:31

    Hi,


    do you have any experiences about the performance of automate generated HTML by XSL & XML


    for example which one is faster:


    1. seperate Asp-Sites
    2. 1 Asp-Site that was generated by 1 XSL-Site (C# XslTransform)


    Thanks
    Schneeblitz

  2. 22 Sep 2005 at 09:42

    how would you be able to that in ASP.Net 2.0.....

  3. 14 Oct 2002 at 06:27
    Generating XML documents and associating them with style sheets is a great way of creating reports.
    The user can view database information, statistics, etc... using plain html text and print them in a neat
    format.
  4. 01 Jan 1999 at 00:00

    This thread is for discussions of Using ADO.NET, XML & XSL to generate HTML.

Leave a comment

Sign in or Join us (it's free).

Bill Burris

Related podcasts

  • A Practical Look at Silverlight 2 Part 1

    Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...

Events coming up

  • Mar 15

    DevWeek 2010

    London, United Kingdom

    DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.

Want to stay in touch with what's going on? Follow us on twitter!