Hi,

Please help me with some idea to create a xsl file to display tree view of any xml as html.

Thanks..!!!

You should create a template that can be called recursively to display all nodes within a node.

Hi..Thanks for the reply..
I tried the xsl given below..This works out fine but i m not able to add a new line.
Can u pls check out if u cud get some clue..

<?xml version="1.0"?><!--showtree.xsl-->
<!DOCTYPE xsl:stylesheet [
<!ENTITY nl "&#xa;"><!--new line sequence-->

<!--remove the following empty declaration if namespaces are to be reported-->
<!ENTITY namespace-reporting ''>

<!--the following declaration will only be used if not previously defined-->
<!ENTITY namespace-reporting '
      <xsl:for-each select="namespace::*">
        <xsl:call-template name="namespace">
          <xsl:with-param name="ancestry" select="$this-ancestry"/>
          <xsl:with-param name="node-pos" select="$this-node-pos"/>
        </xsl:call-template>
      </xsl:for-each>
'>
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">
             <!--set the following non-empty to reveal element's node values-->
<xsl:param name="show-element-values"/>

<!--========================================================================-->
<!--nodes with children-->
<xsl:template match="/">
    <HTML>
      <HEAD>
     </HEAD>
      <BODY>
        <H1>FIST Scenario Report</H1>
        <UL>
          <xsl:apply-templates select="node()"/>
        </UL>
      </BODY>
    </HTML>
  </xsl:template>  

<xsl:template match="*">                                      <!--an element-->
  <xsl:param name="ancestry"/>
<xsl:param name="node-pos"/>
  <xsl:variable name="this-ancestry">
    <xsl:if test="$ancestry!=''">
      <xsl:value-of select="$ancestry"/><xsl:text>,</xsl:text>
    </xsl:if>
    <xsl:value-of select="name(.)"/>
  </xsl:variable>

  <xsl:variable name="this-node-pos">
    <xsl:if test="$node-pos!=''">
      <xsl:value-of select="$node-pos"/><xsl:text>.</xsl:text>
    </xsl:if>
    <p><xsl:value-of select="position()"/></p>
  </xsl:variable>
  <xsl:call-template name="node">
    <xsl:with-param name="ancestry" select="$ancestry"/>
    <xsl:with-param name="node-pos" select="$this-node-pos"/>
    <xsl:with-param name="type">Element</xsl:with-param>
    <xsl:with-param name="contents">                          <!--node value-->
      <xsl:choose>
        <xsl:when test="not($show-element-values)"/>
        <xsl:when test="node()">
          <xsl:text>{</xsl:text>
          <p><xsl:value-of select="."/></p>
          <xsl:text>}</xsl:text>,<BR/>
    <xsl:text>
    </xsl:text>
        </xsl:when>
        <xsl:otherwise>no child nodes</xsl:otherwise>
      </xsl:choose>
 <xsl:text> </xsl:text>
      &namespace-reporting;           <!--defined in a general entity in DTD-->
      <xsl:for-each select="@*">
        <xsl:call-template name="attribute">
          <xsl:with-param name="ancestry" select="$this-ancestry"/>
          <xsl:with-param name="node-pos" select="$this-node-pos"/>
        </xsl:call-template>
      </xsl:for-each>
      <xsl:choose>                                         <!--node children-->
        <xsl:when test="node()">
          <xsl:apply-templates select="node()">
            <xsl:with-param name="ancestry" select="$this-ancestry"/>
            <xsl:with-param name="node-pos" select="$this-node-pos"/>
          </xsl:apply-templates>
        </xsl:when>
        <xsl:when test="$show-element-values"/>    <!--content already shown-->
      </xsl:choose>
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>

<!--========================================================================-->
<!--for every leaf node, show the value in report-->

<!--child leaf nodes-->

<xsl:template match="comment()">                               <!--a comment-->
  <xsl:param name="ancestry"/>
  <xsl:param name="node-pos"/>
  <xsl:variable name="this-node-pos">
    <xsl:if test="$node-pos!=''">
      <xsl:value-of select="$node-pos"/><xsl:text>.</xsl:text>
    </xsl:if>
    <xsl:value-of select="position()"/>
  </xsl:variable>
  <xsl:call-template name="node">
    <xsl:with-param name="ancestry" select="$ancestry"/>
    <xsl:with-param name="node-pos" select="$this-node-pos"/>
    <xsl:with-param name="type">Comment</xsl:with-param>
    <xsl:with-param name="contents">
      <p><xsl:text>{</xsl:text><xsl:value-of select="."/><xsl:text>}</xsl:text></p>,<BR/>
<xsl:text>
</xsl:text>
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template match="processing-instruction()">       <!--a processing inst.-->
  <xsl:param name="ancestry"/>
  <xsl:param name="node-pos"/>
  <xsl:variable name="this-node-pos">
    <xsl:if test="$node-pos!=''">
      <xsl:value-of select="$node-pos"/><xsl:text>.</xsl:text>
    </xsl:if>
    <xsl:value-of select="position()"/>
  </xsl:variable>
  <xsl:call-template name="node">
    <xsl:with-param name="ancestry" select="$ancestry"/>
    <xsl:with-param name="node-pos" select="$this-node-pos"/>
    <xsl:with-param name="type">Proc. Inst.</xsl:with-param>
    <xsl:with-param name="contents">
      <xsl:text>{</xsl:text><xsl:value-of select="."/><xsl:text>}</xsl:text>,<BR/>
<xsl:text>
</xsl:text>
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template match="text()">                                <!--a text node-->
  <xsl:param name="ancestry"/>
  <xsl:param name="node-pos"/>
  <xsl:variable name="this-node-pos">
    <xsl:value-of select="$node-pos"/><xsl:text>.</xsl:text>
    <xsl:value-of select="position()"/>
  </xsl:variable>
  <xsl:call-template name="node">
    <xsl:with-param name="ancestry" select="$ancestry"/>
    <xsl:with-param name="node-pos" select="$this-node-pos"/>
    <xsl:with-param name="type">Text</xsl:with-param>
    <xsl:with-param name="contents">
      <xsl:text>{</xsl:text><xsl:value-of select="."/><xsl:text>}</xsl:text>,<BR/>
<xsl:text>
</xsl:text>
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>

<!--attached leaf nodes-->

<xsl:template name="attribute">  
  <xsl:param name="node-pos"/>
                           <!--an attribute-->
  <xsl:variable name="ancestry"/><!--change to xsl:param to include ancestry-->
  <xsl:variable name="this-node-pos">
    <xsl:value-of select="$node-pos"/><xsl:text>.</xsl:text>
    <xsl:number value="position()" format="A"/>
  </xsl:variable>
  <xsl:call-template name="node">
    <xsl:with-param name="ancestry" select="$ancestry"/>
    <xsl:with-param name="node-pos" select="$this-node-pos"/>
    <xsl:with-param name="type">Attribute </xsl:with-param>
    <xsl:with-param name="contents">
<xsl:text>&nl;</xsl:text>
      <xsl:text>{</xsl:text><xsl:value-of select="."/><xsl:text>}</xsl:text>,<BR/>
<xsl:text>
</xsl:text>
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template name="namespace">                        <!--a namespace node-->
 <xsl:param name="node-pos"/>
  <xsl:variable name="ancestry"/><!--change to xsl:param to include ancestry-->

  <xsl:variable name="this-node-pos">
    <xsl:value-of select="$node-pos"/><xsl:text>.</xsl:text>
    <xsl:number value="position()" format="I"/>
  </xsl:variable>
  <xsl:call-template name="node">
    <xsl:with-param name="ancestry" select="$ancestry"/>
    <xsl:with-param name="node-pos" select="$this-node-pos"/>
    <xsl:with-param name="type">Namespace</xsl:with-param>
    <xsl:with-param name="contents">
<xsl:text>&nl;</xsl:text>
      <xsl:text>{</xsl:text><xsl:value-of select="."/><xsl:text>}</xsl:text>,<BR/>
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>

<!--========================================================================-->
<!--display the content of a node-->

<xsl:template name="node">
  <xsl:param name="ancestry"/>
  <xsl:param name="node-pos"/>
  <xsl:param name="type"/>
  <xsl:param name="contents"/>
  <xsl:text>&nl;</xsl:text>
  <xsl:value-of select="$node-pos"/>                     <!--report location-->
  <xsl:text>  </xsl:text>
    <xsl:text>&nl;</xsl:text>
  <xsl:value-of select="$type"/>                               <!--node type-->
  <xsl:if test="not(name(.)='')">                   <!--node name (if appl.)-->
    <xsl:text> '</xsl:text>
    <xsl:if test="namespace-uri(.)!=''">  
                <!--with namespace-->
<xsl:text>
</xsl:text>
      <xsl:text>  {</xsl:text>
      <xsl:value-of select="namespace-uri(.)"/>
      <xsl:text>}  </xsl:text>,<BR/>
<xsl:text>
</xsl:text>

    </xsl:if>
    <xsl:value-of select="name(.)"/>
    <xsl:text>'</xsl:text>
  </xsl:if>                                                            <!--node context-->
  <xsl:if test="$ancestry"> (<xsl:value-of select="$ancestry"/>)</xsl:if>
  <xsl:text> ==== </xsl:text>
  <xsl:value-of select="$contents"/>
</xsl:template>

</xsl:stylesheet>

This works out fine but i m not able to add a new line.
Can u pls check out if u cud get some clue..

What do you mean by adding a new line ?

In total I need to display all the elements of my xml in a tree format to a html page. But here i get all the elements from the xml but the output in the html page is not in a proper format.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.