Click here to Skip to main content
11,634,815 members (69,431 online)
Click here to Skip to main content

Transform XML using XSLT to XHTML treeview

, 15 Jan 2003 113.7K 1.2K 40
Rate this:
Please Sign up or sign in to vote.
Transform XML using XSLT to XHTML treeview.
<!-- Download Links --> <!-- Add the rest of your HTML here -->

Introduction

Transform xml data with xslt to xhtml treeview.

sampletree.xml
<code>
<?xml version="1.0" encoding="UTF-8"?>
<treeview title="Sample tree">
  
  <custom-parameters>
    <param name="shift-width" value="15"/>
    <param name="img-directory" value="images/"/>
  </custom-parameters>
  
<folder title="Desktop" img="Desktop.gif">
  <folder title="My documents" img="MyDocuments.gif">
    <folder title="My music" img="folder.gif">
      <leaf title="siteclx" url="1" img="page.gif"/>
      <leaf title="design" url="2" img="page.gif"/>
      <leaf title="devellopment" url="3" img="page.gif"/>
    </folder>
    <folder title="My images" img="MyImages.gif">
      <leaf title="php" url="4" img="red_ball.gif"/>
      <leaf title="perl" url="5" img="red_ball.gif"/>
    </folder>
  </folder>
  <folder title="My computer" img="computer.gif">
    <folder title="Floppy (A:)" url="1" img="floppy.gif"/>
    <folder title="Hard drive (C:)" url="1" img="harddrive.gif">
      <leaf title="Perl" url="7" img="page.gif"/>
      <leaf title="Java" url="8" img="page.gif"/>
      <leaf title="PHP" url="8" img="page.gif"/>
      <leaf title="dotNet" url="8" img="page.gif"/>           
    </folder>
    <leaf title="Configuration Pannel" url="9" img="config.gif"/>
    <leaf title="Shared documents" url="10" img="folder.gif"/>
  </folder>
  <folder title="Network" img="network.gif"/>
  <folder title="Trash" img="trash.gif"/>
</folder> 
</treeview>
</CODE>

treeview.xslt

<code>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:asp="remove">

  <xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="yes"/> 
  <xsl:variable name="shift-width" select="/treeview/custom-parameters/param[@name='shift-width']/@value"/>
  <xsl:variable name="img-directory" select="/treeview/custom-parameters/param[@name='img-directory']/@value"/>
  <xsl:template match="/treeview">

  <table border="0" cellspacing="0" cellpadding="0">
      <tr><td>
      <xsl:apply-templates select="folder">
        <xsl:with-param name="depth" select="1"/>
      </xsl:apply-templates>
    </td></tr>
   </table>
        
  </xsl:template>

  <xsl:template match="folder">
    <xsl:param name="depth"/>
      <table border="0" cellspacing="0" cellpadding="0">
        <tr>
          <xsl:if test="$depth>1">
            <td width="{$shift-width}"></td>
          </xsl:if>
          <td valign="top">
            <a onclick="toggle(this)" class="folder">
            <img src="{$img-directory}plus.gif"/>
            <img src="{$img-directory}{@img}"/><xsl:value-of select="@title"/></a>
            <div>
              <xsl:attribute name="style">display:none;</xsl:attribute>
              <xsl:apply-templates select="folder">
                <xsl:with-param name="depth" select="$depth+1"/>
              </xsl:apply-templates>
              <xsl:apply-templates select="leaf"/>
            </div>
          </td>
        </tr>
      </table>
  </xsl:template>
  
  <xsl:template match="leaf">
    <table border="0" cellspacing="0" cellpadding="0">
      <tr>
      <td width="{$shift-width}"></td> 
        <td valign="top">
          <a class="leaf"><xsl:attribute name="onclick">selectLeaf('<xsl:value-of select="@title"/>','<xsl:value-of select="@url"/>')</xsl:attribute>
            <img src="{$img-directory}{@img}"/>
            <xsl:value-of select="@title"/>
          </a>
        </td>
      </tr>
      </table>
  </xsl:template>
  
</xsl:stylesheet>
</CODE>

transform.aspx

<code>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ import Namespace="System.Xml.XPath" %>
<%@ import Namespace="System.Xml.Xsl" %>
<%@ import Namespace="System.IO" %>
<script language="C#" runat="server" Debug="true">

/* 

(c) COPYRIGHT 2002 SiteCLX
ALL RIGHTS RESERVED

author:         P.A. Huisman
filename:       transform.aspx
discription:    C# Sample, transform xml data with xslt to xhtml treeview.

modification log:
 version        date            by      notes
 1.00           16/01/2003      PAH     first version.

*/

void Page_Load(Object sender, EventArgs e) {

   XPathDocument treeDoc = new XPathDocument(Server.MapPath("sample.xml"));
   XslTransform treeView = new XslTransform();
   treeView.Load(Server.MapPath("treeview.xslt"));
   
   StringWriter sw = new StringWriter();
   treeView.Transform(treeDoc, null, sw);
   string result = sw.ToString();
   result = result.Replace("xmlns:asp=\"remove\"", "");
  
   Control ctrl = Page.ParseControl(result);
   myTree.Controls.Add(ctrl);

}
</script>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Peter Huisman
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionHow about with a SQL Server XML Stream? Pin
dotnet_rules12-Mar-03 5:59
memberdotnet_rules12-Mar-03 5:59 
AnswerRe: How about with a SQL Server XML Stream? Pin
Anonymous25-May-05 5:17
sussAnonymous25-May-05 5:17 
GeneralModification Request... Pin
blong16-Jan-03 9:01
memberblong16-Jan-03 9:01 
GeneralRe: Modification Request... Pin
Marc Clifton16-Jan-03 10:59
memberMarc Clifton16-Jan-03 10:59 
Questionwhat are you trying to show ? Pin
.S.Rod.16-Jan-03 8:21
member.S.Rod.16-Jan-03 8:21 
AnswerRe: what are you trying to show ? Pin
Peter Huisman16-Jan-03 8:46
memberPeter Huisman16-Jan-03 8:46 
AnswerRe: what are you trying to show ? Pin
Peter Huisman16-Jan-03 8:51
memberPeter Huisman16-Jan-03 8:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150728.1 | Last Updated 16 Jan 2003
Article Copyright 2003 by Peter Huisman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid