Subject: Re: [xsl] most efficient flat file listing to hierarchical From: James Fuller <jim.fuller@xxxxxxxxxxxxxx> Date: Thu, 11 Jan 2007 17:38:51 +0100 |
this is an elegant template, really shows the power and sweetspot hit by XSLT 2.0 (anyone doing this in XSLT 1.0, barring FXSL, this becomes a bit of a nightmare)....any alternate approaches which makes things more efficient (i dont think its possible to optimise the MKay recursive approach).
Why bar FXSL -- are we masochistic? :oO)
very nice...and extremely readable.Here's my take on this problem. Trying to adhere to the KISS principle, I'm using a two-step approach naturally expressed as functional composition.
Notice that not only is this a litlle easier to understand, but that the main "powerhorse" (the function f:makeTree() ) is just 17 lines of code:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:f="http://fxsl.sf.net/" exclude-result-prefixes="f xs">
<xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="/*"> <xsl:sequence select="f:makeTree(f:tokenizePaths(/*/*), 1)"/> </xsl:template>
<xsl:function name="f:tokenizePaths"> <xsl:param name="pfilePaths" as="element()+"/> <xsl:for-each select="$pfilePaths"> <item> <xsl:for-each select="tokenize(.,'/')"> <q><xsl:value-of select="."/></q> </xsl:for-each> </item> </xsl:for-each> </xsl:function>
<xsl:function name="f:makeTree" as="element()*"> <xsl:param name="pItems" as="element()*"/> <xsl:param name="pqLevel" as="xs:integer"/>
<xsl:for-each-group select="$pItems" group-by="q[$pqLevel]">
<xsl:choose>
<xsl:when test="count(current-group()) = 1">
<file name="{q[$pqLevel]}"/>
</xsl:when>
<xsl:otherwise>
<dir name="{q[$pqLevel]}">
<xsl:sequence select="f:makeTree(current-group(), $pqLevel+1)"/>
</dir>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:function>
</xsl:stylesheet>
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] most efficient flat file , Dimitre Novatchev | Thread | Re: [xsl] most efficient flat file , Abel Braaksma |
Re: [xsl] most efficient flat file , David Carlisle | Date | Re: [xsl] Converting XSLT 2.0 Group, Vincent Ng |
Month |