View Single Post
 
Old October 18th, 2006, 11:43 AM
mhkay's Avatar
mhkay mhkay is offline
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You've started with a very procedural description of an algorithm:

>"For-each" through all of the elements. Create the necessary[list] tag. Determine the depth of each of the elements. Then, compare that depth to the previous element's depth. If current element - previous element = 0, then create another <li>--

This isn't usually a good starting point for XSLT coding. It's much better to try to describe the output as a function of the input: "for every element in the input there must be a[list] in the output and for each of its children there must be a corresponding <li>..."

I'm having trouble reverse engineering your requirements from your procedural solution. But you seem to be doing a very direct translation of one hierarchy into another, and it should be possible to describe this entirely using template rules. Try to describe it in these terms: "for an element with children, I want a[list] in the output with each of its children wrapped in an <li>". For an element without children, I want...

Then you can translate this directly into template rules:

<xsl:template match="*[*]"> [list]
  <xsl:for-each select="*">
    <li><xsl:apply-templates select="."/></li>

<xsl:template match="*[not(*)]">
  <xsl:value-of select="."/>
</xsl:template>

and so on.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference