Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert XML to a tree in html

Status
Not open for further replies.

tommytrance

Programmer
Sep 9, 2003
1
GB
Hi Folks,
I am creating an XML doc from an ORacle database. It has a tree structure up to 4 levels deep. It works perfectly. What I need to do though is to convert it into an HTML file that still represents the hierarchy. My users want to be able to expand the tree elements they want to see and then print the result. I could do this in XML but they will see the tags.

So, basic quesion is........

How do I convert an XML doc into a HTML tree (if such a thing exists)

Cheers

Tom Mcguire
Arete Software Ltd
 
So you have two issues. How to do an HTML tree is the main one. Once you know what HTML you need to represent your data then writing the XSL(T) should be fairly simple. I have done an HTML tree for IE but not a cross browser one and I really REALLY don’t want to [lol]

-pete
 
Uh... of course you can use an xsl-stylesheet to build that tree, (it's not that difficult, but a tedious job) but did you notice that you can open an xml-document in IE?

Anyway, if you really need an xsl-stylesheet to build a treeview let me know, I might have saved an attempt to do so somewhere in a dark corner.
 
Soory, was a bit in a hurry. Anyway, here's an experiment I once did on making a treeview in HTML. Hope you can make something out of it.

<?xml version=&quot;1.0&quot;?>
<xsl:stylesheet xmlns:xsl=&quot; version=&quot;1.0&quot;>
<xsl:variable name=&quot;intInput&quot; select=&quot;9&quot;/>
<!--
find upper elements
-->
<xsl:template match=&quot;obj_tabel&quot;>
<script type=&quot;text/javascript&quot;>
function TOGGLE(id)
{
if (eval('subs'+id+'.style.display')==''){
eval('subs'+id+'.style.display=&quot;none&quot;');
}
else{
eval('subs'+id+'.style.display=&quot;&quot;');
};
if (eval(&quot;IM&quot;+id+&quot;.src&quot;).match(&quot;closed.gif&quot;,&quot;g&quot;)!=null){
eval('IM'+id+'.src=&quot;open.gif&quot;');
};
else {
if (eval(&quot;IM&quot;+id+&quot;.src&quot;).match(&quot;open.gif&quot;,&quot;g&quot;)!=null){
eval('IM'+id+'.src=&quot;closed.gif&quot;');
};
}
}
</script>
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match=&quot;obj_collectie&quot;>
<table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;>
<xsl:for-each select=&quot;obj_object&quot;>
<xsl:sort select=&quot;obj_id&quot; data-type=&quot;number&quot;/>
<xsl:if test=&quot;obj_ouder_obj_id=0&quot;>
<xsl:call-template name=&quot;MakeBranche&quot;>
<xsl:with-param name=&quot;col&quot; select=&quot;parent::eek:bj_collectie&quot;></xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:for-each>
</table>
</xsl:template>
<!--
template for object: draws all nodes, starting with rootnode
-->
<xsl:template name=&quot;MakeBranche&quot;>
<xsl:param name=&quot;StuffBefore&quot; select=&quot;''&quot;/>
<xsl:param name=&quot;col&quot; select=&quot;.&quot;></xsl:param>
<xsl:variable name=&quot;ouder&quot; select=&quot;obj_id&quot;/>
<!-- draw this node -->
<table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;>
<tr>
<xsl:variable name=&quot;StuffBefore2&quot; select=&quot;$StuffBefore&quot;/>
<xsl:call-template name=&quot;StuffBefore&quot;>
<xsl:with-param name=&quot;StuffBefore2&quot; select=&quot;$StuffBefore&quot;/>
</xsl:call-template>
<a>
<xsl:variable name=&quot;ID&quot; select=&quot;obj_id&quot;/>
<xsl:attribute name=&quot;onclick&quot;><xsl:text>TOGGLE(</xsl:text><xsl:value-of select=&quot;$ID&quot;/><xsl:text>)</xsl:text></xsl:attribute>
<td width=&quot;11&quot;>
<xsl:choose>
<xsl:when test=&quot;count($col/obj_object[obj_ouder_obj_id=$ouder])=0&quot;>
<img src=&quot;leaf.gif&quot;>
<xsl:attribute name=&quot;id&quot;>IM<xsl:value-of select=&quot;$ID&quot;/></xsl:attribute>
</img>
</xsl:when>
<xsl:eek:therwise>
<img src=&quot;closed.gif&quot;>
<xsl:attribute name=&quot;id&quot;>IM<xsl:value-of select=&quot;$ID&quot;/></xsl:attribute>
</img>
</xsl:eek:therwise>
</xsl:choose>
</td>
<td><xsl:text> </xsl:text><xsl:value-of select=&quot;obj_omschrijving&quot;/>
</td>
</a>
</tr>
</table>
<!-- draw subs; first the ones that are have childnodes -->
<div>
<xsl:attribute name=&quot;id&quot;><xsl:text>subs</xsl:text><xsl:value-of select=&quot;obj_id&quot;/></xsl:attribute>
<xsl:attribute name=&quot;style&quot;><xsl:text>display='none'</xsl:text></xsl:attribute>
<xsl:variable name=&quot;number&quot; select=&quot;count($col/obj_object[obj_ouder_obj_id=$ouder])&quot;></xsl:variable>
<xsl:variable name=&quot;count&quot; select=&quot;0&quot;></xsl:variable>
<table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;>
<xsl:for-each select=&quot;$col/obj_object[obj_ouder_obj_id=$ouder]&quot;>
<xsl:sort select=&quot;obj_id&quot; data-type=&quot;number&quot;/>
<xsl:variable name=&quot;child&quot; select=&quot;obj_id&quot;></xsl:variable>
<xsl:if test=&quot;count($col/obj_object[obj_ouder_obj_id=$child])>0&quot;>
<xsl:choose>
<xsl:when test=&quot;$number=$count&quot;>
<xsl:call-template name=&quot;MakeBranche&quot;>
<xsl:with-param name=&quot;StuffBefore&quot; select=&quot;concat($StuffBefore,'A')&quot;/>
<xsl:with-param name=&quot;col&quot; select=&quot;$col&quot;></xsl:with-param>
</xsl:call-template>

</xsl:when>
<xsl:eek:therwise>
<xsl:call-template name=&quot;MakeBranche&quot;>
<xsl:with-param name=&quot;StuffBefore&quot; select=&quot;concat($StuffBefore,'B')&quot;/>
<xsl:with-param name=&quot;col&quot; select=&quot;$col&quot;></xsl:with-param>
</xsl:call-template>
</xsl:eek:therwise>
</xsl:choose>

</xsl:if>
</xsl:for-each>
<xsl:for-each select=&quot;$col/obj_object[obj_ouder_obj_id=$ouder]&quot;>
<xsl:sort select=&quot;obj_id&quot; data-type=&quot;number&quot;/>
<xsl:variable name=&quot;child&quot; select=&quot;obj_id&quot;></xsl:variable>
<xsl:if test=&quot;count($col/obj_object[obj_ouder_obj_id=$child])=0&quot;>
<xsl:choose>
<xsl:when test=&quot;$number=$count&quot;>
<xsl:call-template name=&quot;MakeBranche&quot;>
<xsl:with-param name=&quot;StuffBefore&quot; select=&quot;concat($StuffBefore,'A')&quot;/>
<xsl:with-param name=&quot;col&quot; select=&quot;$col&quot;></xsl:with-param>
</xsl:call-template>

</xsl:when>
<xsl:eek:therwise>
<xsl:call-template name=&quot;MakeBranche&quot;>
<xsl:with-param name=&quot;StuffBefore&quot; select=&quot;concat($StuffBefore,'B')&quot;/>
<xsl:with-param name=&quot;col&quot; select=&quot;$col&quot;></xsl:with-param>
</xsl:call-template>
</xsl:eek:therwise>
</xsl:choose>
$number = $number+1

</xsl:if>
</xsl:for-each>



</table>
</div>
</xsl:template>
<!--
StuffBefore
-->
<xsl:template name=&quot;StuffBefore&quot;>
<xsl:param name=&quot;StuffBefore2&quot; select=&quot;''&quot;/>
<!-- first character of StuffBefore -->
<xsl:if test=&quot;string-length($StuffBefore2)>1&quot;>
<!-- not yet last cell in StuffBefore -->
<xsl:if test=&quot;starts-with($StuffBefore2,'A')&quot;>
<!-- vertical line -->
<td width=&quot;11&quot;>
<img src=&quot;geenclosed.gif&quot;/>
</td>
</xsl:if>
<xsl:if test=&quot;starts-with($StuffBefore2,'B')&quot;>
<!-- nothing at all -->
<td width=&quot;11&quot;/>
</xsl:if>
</xsl:if>
<xsl:if test=&quot;string-length($StuffBefore2)=1&quot;>
<!-- last cell StuffBefore -->
<xsl:if test=&quot;starts-with($StuffBefore2,'A')&quot;>
<!-- side branche -->
<td width=&quot;11&quot;>
<img src=&quot;zijclosed.gif&quot;/>
</td>
</xsl:if>
<xsl:if test=&quot;starts-with($StuffBefore2,'B')&quot;>
<!-- last branching -->
<td width=&quot;11&quot;>
<img src=&quot;eindclosed.gif&quot;/>
</td>
</xsl:if>
</xsl:if>
<xsl:if test=&quot;string-length($StuffBefore2)>1&quot;>
<!-- and do the rest of StuffBefore -->
<xsl:call-template name=&quot;StuffBefore&quot;>
<xsl:with-param name=&quot;StuffBefore2&quot; select=&quot;substring($StuffBefore2,2)&quot;/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
 
Looking at this mess you might wonder: 'why didn't he finish it proprly?'. The answer is simple: I foud it much easier to dump xml in an ActiveX-treeview-control. ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top