Click here to Skip to main content
14,576,970 members

Transform XML using XSLT to XHTML treeview

Rate this:
2.18 (19 votes)
Please Sign up or sign in to vote.
2.18 (19 votes)
15 Jan 2003
Transform XML using XSLT to XHTML treeview.


Transform xml data with xslt to xhtml treeview.

<?xml version="1.0" encoding="UTF-8"?>
<treeview title="Sample tree">
    <param name="shift-width" value="15"/>
    <param name="img-directory" value="images/"/>
<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 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 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"/>           
    <leaf title="Configuration Pannel" url="9" img="config.gif"/>
    <leaf title="Shared documents" url="10" img="folder.gif"/>
  <folder title="Network" img="network.gif"/>
  <folder title="Trash" img="trash.gif"/>


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"  xmlns:xsl="" 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">
      <xsl:apply-templates select="folder">
        <xsl:with-param name="depth" select="1"/>

  <xsl:template match="folder">
    <xsl:param name="depth"/>
      <table border="0" cellspacing="0" cellpadding="0">
          <xsl:if test="$depth>1">
            <td width="{$shift-width}"></td>
          <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>
              <xsl:attribute name="style">display:none;</xsl:attribute>
              <xsl:apply-templates select="folder">
                <xsl:with-param name="depth" select="$depth+1"/>
              <xsl:apply-templates select="leaf"/>
  <xsl:template match="leaf">
    <table border="0" cellspacing="0" cellpadding="0">
      <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"/>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<%@ 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

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();
   StringWriter sw = new StringWriter();
   treeView.Transform(treeDoc, null, sw);
   string result = sw.ToString();
   result = result.Replace("xmlns:asp=\"remove\"", "");
   Control ctrl = Page.ParseControl(result);


<html xmlns="" xml:lang="en" lang="en">
    <title>SiteCLX title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="imagetoolbar" content="no" />
    <meta http-equiv="imagetoolbar" content="false" />
    <meta name="author" content="P.A. Huisman" />
    <meta name="owner" content="SiteCLX" /> 
    <meta name="copyright" content="copyright 2002-2003 SiteCLX."  />
    <meta name="keyphrases" content="xhtml treeview sample" /> 
    <meta name="resource-type" content="document" /> 
    <meta name="rating" content="general" /> 
    <meta name="keywords" content="xhtml, treeview" />
    <meta name="description" content="xhtml treeview sample" />
    <meta name="abstract" content="xhtml treeview sample" />
    <meta name="robots" content="index,follow" />
    <meta name="revisit-after" content="30 days" />
    <style type="text/css" media="screen">
    .leaf {
      font-family : verdana;
      cursor: hand;
      font-size : 8pt;
      color: #000000;
    .leaf:hover {text-decoration:undeline; cursor: hand;}
    <script type="text/javascript" language="javascript">
    function toggle(node) {
      var nextDIV = node.nextSibling;
      while(nextDIV.nodeName != "DIV") {
        nextDIV = nextDIV.nextSibling;
      if ( == 'none') {
        if (node.childNodes.length > 0) {
          if (node.childNodes.item(0).nodeName == "IMG") {
            node.childNodes.item(0).src = getImgDirectory(node.childNodes.item(0).src) + "minus.gif";
        } = 'block';
      else {
        if (node.childNodes.length > 0) {
          if (node.childNodes.item(0).nodeName == "IMG") {
              node.childNodes.item(0).src = getImgDirectory(node.childNodes.item(0).src) + "plus.gif";
        } = 'none';
    function getImgDirectory(source) {
        return source.substring(0, source.lastIndexOf('/') + 1);
    function selectLeaf(title, url) {
      alert("You just clicked on title = " + title + ":: url = " + url);
    <asp:label id ="myTree" runat="server" />


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


About the Author

Peter Huisman
Netherlands Netherlands
No Biography provided

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
MemberAnonymous25-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
mvaMarc Clifton16-Jan-03 10:59 
Questionwhat are you trying to show ? Pin
Stephane Rodriguez.16-Jan-03 8:21
MemberStephane Rodriguez.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    Praise Praise    Rant Rant    Admin Admin   

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

Posted 15 Jan 2003


40 bookmarked