Click here to Skip to main content
15,350,252 members
Articles / Web Development / XHTML
Posted 29 Jul 2009


19 bookmarked

A well look Html Tree using XML&XSLT

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
29 Jul 2009CPOL2 min read
A well look Html Tree using XML&XSLT


There are many web based tree controls, such as dthmlxtree(published by Saint-Petersburg, Russia), xtree(published by WebFX) and Microsoft DeepTree used in msdn. These controls let web based applications as convenience as C/S applications. But these controls have limitations such as coding is complicate, hard to modify and so on. This article will introduce how to build a web based tree control step by step using xml&xslt, and how the key features works.


1. web based using xml&xslt.
2. light weight, all codes less than 13k(images excluded).
3. xp explorer style operations, support keyboard navigation.
4. support IE, firefox, not tested on opera and other browsers.
5. simple data file, conveniently generated from database.

Features to be implemented

1. edit node title in place.
2. drag&drop or cut&paste (within one tree, between trees).
3. Dynamic Loading for big trees.
4. check boxes.

Using the code

Build xml data file as following structure:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="xtree.xsl" ?>
In this xml file, href="/KB/HTML/xtree/xsl" directive lead xml processor to format xml by using xtree.xsl. Be aware directory “imgs” is required.

Implementation Details


In xtree.xsl, there is 4 parts: main html template, tree node template, css and javascript. The core is tree node template written by XSL. This template is a recursive call procedure, just look like this:
procedure formatTreeNode(xmlTreeNode) {
    foreach(childNode in xmlTreeNode.childNodes) {
        //recursive call

The real xslt code look like this:

<xsl:template name="tree">
<xsl:param name="tree-node" />
<xsl:for-each select="$tree-node">
<div class="tree-node">
<div class="node-content">
<!-- node content format -->
<xsl:if test="$children>0">
<!--recursive call -->

buildNodeHTML generate such html code as following content(leaf node, no child):

<div class="tree-node"> 
<div class="node-content"> 
<img class="img-line" onclick="onClick(this)" src="imgs/T.gif"> 
<img class="img-icon" onclick="onClick(this)" src="imgs/leaf4.gif"> 
<span onmouseover="onMouseOver(this)" onmouseout="onMouseOut(this)" onmousedown="onMouseDown(this);" onclick="onTextClick(this)"> node1</span> 

HTML from above display a node status mark(plus or minus), a node icon and node tile, and connect event processor. This is a single node prototype. If this node have a child, codes as following is inserted:

<div class="sub-container"> 
<div class="sub-padding"> 
        <div class="tree-node"></div> 

<div class=" tree-node "> is a big area to hold node’s own content and all of it’s children, <div class=" sub-container"> is a smaller area to hold all children, <div class=" sub- padding "> is another smaller area to hold all children which are indented from their parent.


published at 7/28/2009.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Product Manager
China China

Comments and Discussions

Questionan Example Pin
brrb20-Aug-12 4:10
Memberbrrb20-Aug-12 4:10 

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.