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>

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Perl - The only language that looks the same before and after RSA encryption.” - Keith Bostic