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

Re: Recursion Examples

Subject: Re: Recursion Examples
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 1 May 2001 08:11:02 +0100
xsl build hashtable recursive
Hi Dan,

That's a nicely constructed solution. There are a just a few ways to
improve it that I thought I'd point out - I hope you don't mind.

The first is that I'm not sure why you're bothering passing round the
$ParentId parameter, especially as you never use it.  The code would
be a lot cleaner if you didn't bother:

<xsl:template match="version">
   <xsl:apply-templates select="module[parentid='root']" />
</xsl:template>
 
<xsl:template match="module">
   <xsl:variable name="Id" select="id"/>
   <xsl:copy>
     <xsl:attribute name="id"><xsl:value-of select="id"/></xsl:attribute>
     <xsl:attribute name="name"><xsl:value-of select="name"/></xsl:attribute>
     <xsl:apply-templates select="parent::*/module[parentid=$Id]" />
   </xsl:copy>
</xsl:template>

The second thing that would make the code simpler would be to create a
new module element rather than copy it.  That way, you could add the
id and name attributes directly and use attribute value templates to
specify their values.  So the second template becomes:

<xsl:template match="module">
   <xsl:variable name="Id" select="id"/>
   <module id="{id}" name="{name}">
     <xsl:apply-templates select="parent::*/module[parentid=$Id]" />
   </module>
</xsl:template>

The third, and most important thing, is that this is a place where you
will benefit a great deal in terms of efficiency from using keys.
Rather than searching through all the module elements for one with a
particular parentid each time, you can tell the processor to build a
hashtable that links a parentid with a module, and then use that
parentid to access the module information.  The key is set up like:

<xsl:key name="modules" match="module" use="parentid" />

Now, I can use the key() function to retrieve the module elements that
have a particular parentid.  For example, I can get the root module
with:

<xsl:template match="version">
   <xsl:apply-templates select="key('modules', 'root')" />
</xsl:template>

And I can get the other modules similarly. In fact, this also means I
can get rid of the variable, since I never change the context node in
the template:

<xsl:template match="module">
   <module id="{id}" name="{name}">
     <xsl:apply-templates select="key('modules', id)" />
   </module>
</xsl:template>

I hope that helps,

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.