Subject: Re: [xsl] Recursion Examples From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx> Date: Tue, 1 May 2001 08:11:02 +0100 |
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Recursion Examples, Dan Diebolt | Thread | RE: [xsl] Recursion Examples, Jon Wynacht |
Re: [xsl] Recursion Examples, Jeni Tennison | Date | RE: [xsl] Multiple recursive transf, Michael Kay |
Month |