cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Luca Morandini" <luca.morandi...@tin.it>
Subject RE: making tree like structure using XML,XSP,XSL
Date Sat, 13 Apr 2002 12:47:21 GMT
Niket,

here you are...

SITEMAP.XMAP (fragment)
<map:pipeline>
	<map:match pattern="hash.xml">
		<map:generate src="documents/hashtable.xml"/>
     		<map:transform src="stylesheets/hashtable.xsl"/>
     		<map:serialize type="xml"/>
	</map:match>
</map:pipeline>

HASHTABLE.XML
<?xml version="1.0"?>
<page>
	<entry id="1"  parentid="0"/>
	<entry id="2"  parentid="0"/>
	<entry id="3"  parentid="0"/>
	<entry id="4"  parentid="0"/>
	<entry id="5"  parentid="1"/>
	<entry id="6"  parentid="1"/>
	<entry id="7"  parentid="6"/>
	<entry id="8"  parentid="7"/>
	<entry id="11" parentid="5"/>
	<entry id="9"  parentid="2"/>
	<entry id="10" parentid="2"/>
	<entry id="12" parentid="5"/>
</page>

HASHTABLE.XSL
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

	<xsl:template match="page">

		<!-- Starts the tree -->
		<xsl:element name="tree">

			<xsl:apply-templates/>

		</xsl:element>

	</xsl:template>

	<!-- Selects main branches -->
	<xsl:template match="//entry[@parentid=0]">

		<xsl:call-template name="process-branch">
			<xsl:with-param name="id"><xsl:value-of select="@id"/></xsl:with-param>
			<xsl:with-param name="parentid"><xsl:value-of
select="@parentid"/></xsl:with-param>
		</xsl:call-template>

	</xsl:template>

	<!-- Recursive function -->
	<xsl:template name="process-branch">
		<xsl:param name="id"/>
		<xsl:param name="parentid"/>

		<xsl:choose>

			<!-- If element has no branches (i.e. is a leaf) -->
			<xsl:when test="count(//entry[@parentid=current()/@id])=0">

				<!-- Writes the "leaf" element -->
				<xsl:element name="leaf">
					<xsl:attribute name="id"><xsl:value-of select="$id"/></xsl:attribute>
				</xsl:element>

		    </xsl:when>

			<!-- If element has branches -->
		    <xsl:otherwise>

				<!-- Starts a "branch" element -->
				<xsl:element name="branch">
					<xsl:attribute name="id"><xsl:value-of select="$id"/></xsl:attribute>

					<!-- For every branches of current branch -->
					<xsl:for-each select="//entry[@parentid=current()/@id]">

						<!-- Recurse the branch processing -->
						<xsl:call-template name="process-branch">
							<xsl:with-param name="id"><xsl:value-of
select="@id"/></xsl:with-param>
							<xsl:with-param name="parentid"><xsl:value-of
select="@parentid"/></xsl:with-param>
						</xsl:call-template>

					</xsl:for-each>

				</xsl:element>

		    </xsl:otherwise>

		</xsl:choose>

	</xsl:template>

</xsl:stylesheet>

Best regards,

---------------------------------------------
               Luca Morandini
               GIS Consultant
              lmorandini@ieee.org
http://utenti.tripod.it/lmorandini/index.html
---------------------------------------------

-----Original Message-----
From: Niket Anand [mailto:niketanand@npi.stpn.soft.net]
Sent: Friday, April 12, 2002 5:13 PM
To: Niket Anand
Subject: making tree like structure using XML,XSP,XSL


Hi All,
I have one function that connect to database and retrive data in form of
Hashtable.
My Hashtable may be like this.
MsgID         ParentID
1                    0
2                    0
3                    0
4                    0
5                    1
6                    1
7                    6
8                    7
9                    2
10                  2
11                  5
12                  5

Tree structure would be like this based on MsgID and ParentID
MsgID will be the Id of a tag and parentid is the id of the parent with
which it is linked.
1(parent0)
|
|
-------5(child of 1)
        |
        |___11(child of 5)
        |
        |____12(child of 5 and sibling of 11)
____6(child of 1 and sibling of 5)
        |
        |____7(child of 6)
                |
                |___8(child of 7)
2(parent 2)
|
|____9(child of 2)
|
|____10(child of 2)

3(parent 3)

4(parent 4)
Like this I have to show dynamic tree like structure in HTML form.I am using
cocoon2.0
This can be either handled by XSP or XSL or both.
Please suggest me how to solve the recursive loop problem using XSL..
How can I apply logic to have dynamic tree structure building function based
on the values of Hashtable keys and values.
Pls suggest  ideas.
Thanks,
Niket


---------------------------------------------------------------------
Please check that your question has not already been answered in the
FAQ before posting. <http://xml.apache.org/cocoon/faqs.html>

To unsubscribe, e-mail: <cocoon-users-unsubscribe@xml.apache.org>
For additional commands, e-mail: <cocoon-users-help@xml.apache.org>


Mime
View raw message