Click here to Skip to main content
15,885,067 members
Articles / Web Development / XHTML

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 24.3K   303   19   1
A well look Html Tree using XML&XSLT

Introduction

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.

Features

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
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="xtree.xsl" ?>
<nodes>
       <node> 
              <code>node1_code</code>
              <parent_code>node0</parent_code>
              <name>node1_name</name>
       </node>
       <node>
              <code>node2_code</code>
              <parent_code>node0</parent_code>
              <name>node2_name</name>
       </node>
       … 
       … 
</nodes>  
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

pic2.PNG

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:
VB.NET
procedure formatTreeNode(xmlTreeNode) {
    foreach(childNode in xmlTreeNode.childNodes) {
        buildNodeHTML(childNode);
        //recursive call
        formatTreeNode(childNode);
}
}

The real xslt code look like this:

ASP.NET
<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 -->
...
</div>
</div>
<xsl:if test="$children>0">
<!--recursive call -->
...
</xsl:if>
</xsl:for-each>

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

ASP.NET
<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> 
</div> 
</div> 

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:

ASP.NET
<div class="sub-container"> 
<div class="sub-padding"> 
        <div class="tree-node"></div> 
</div> 
</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.

History

published at 7/28/2009.

License

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


Written By
Product Manager
China China
Neeker
Beijing

Comments and Discussions

 
Questionan Example Pin
brrb20-Aug-12 4:10
brrb20-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.