Re: [xsl] most efficient flat file listing to hierarchical

Subject: Re: [xsl] most efficient flat file listing to hierarchical
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Thu, 11 Jan 2007 21:27:28 +0100
Dimitre Novatchev wrote:
Not to be picky, but following your own gudelines I count 16 lines in
your code ? ?

You are just a better counter than I am ;-). I happened to count a later version, which was without the redundant delete-template for "item" nodes, and I did not count xsl:stylesheet and xsl:output.


But since this is an XSLT list, here's the XSLT solution of formalizing my "guidelines" for counting, which can simply be added to any stylesheet, call it on itself (I mean, call the stylesheet with as input the stylesheet itself), and it will correctly show the number of lines in a uniform way (provided I did not make any mistakes in the formula). Using it on my solution, gives 12 lines, on the solution I posted here earlier, gives 14 lines.

   <xsl:template match="/xsl:stylesheet">
       <xsl:value-of select="
           count(.//*[not(contains(@match, 'xsl:'))][*]) * 2 +
           count(.//*[not(contains(parent::*/@match, 'xsl:'))][not(*)])" />
   </xsl:template>

It's no rocket science, but here's a little explanation:
In essence, it counts all nodes with children and doubles it, then adds all nodes without children, like this:
count(//*[*]) * 2 + count(//*[not(*)])


The version in the template ignores the root node and excludes itself (the xsl:template that you have to add) from the count by requesting only nodes that do not have a @match attribute with 'xsl:' in its text.

It should be easy to change the xpath to allow other nodes in the count (text nodes, comment nodes etc).

-- Abel Braaksma
  http://www.nuntia.nl

Current Thread