[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Generate Hierarchical Tree from non-hierarchical XML

Subject: RE: Generate Hierarchical Tree from non-hierarchical XML
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Thu, 17 Aug 2000 21:31:20 +0100
xsl hierarchical tree
At 18:42 17/08/00 +0100, Ben Robb wrote:
>Try:
>
><xsl:template match="employees">
>	<table><xsl:apply-templates select="employee[manager = '']
>"/></table>
></xsl:template>
>
><xsl:template match="employee">
>	<tr><td width="150"></td><td>
>		<xsl:value-of select="name" />
>		<xsl:if test="../employee[manager != current()/id]">
>			<table>
>				<xsl:apply-templates
>select="../employee[manager = current()/id] ">
>					<xsl:sort select="id"/>
>				</xsl:apply-templates>
>			</table>
>		</xsl:if>
>	</td></tr>
></xsl:template>

If you are using an XSLT Processor that supports keys, you could also use
keys to quickly access the employees that you're interested in.  Define a
key that indexes the employees by their manager:

  <xsl:key name="employees" match="employee" use="manager" />

and you can then use key('employees', $id) to get the employee with the
relevant id.  Once this key is defined, the above template becomes:

<xsl:template match="employee">
  <tr><td width="150"></td><td>
    <xsl:value-of select="name" />
    <xsl:if test="key('employees', id)">
      <table>
        <xsl:apply-templates select="key('employees', id)">
          <xsl:sort select="id"/>
        </xsl:apply-templates>
      </table>
    </xsl:if>
  </td></tr>
</xsl:template>

Using key() is more efficient if you have a large dataset because it stops
the XSLT Processor from having to search through all the nodes to find the
relevant employees: they're already collected together for easy access.

Cheers,

Jeni

Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.