View Single Post
 
Old October 18th, 2006, 11:18 AM
knc knc is offline
Registered User
 
Join Date: Oct 2006
Location: , , USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Hierarchical HTML unordered lists from any XML

Hi. I am a newbie at XSLT. I have done some other programming, but I have to admit, I am not very good. I have Kay's book XSLT 2.0 and am reading through some examples. My code ultimately will go into PHP code, which, I think, uses Sablotron, v 1.0. I am currently using a testing application called TextXSLT on Mac OS X.

My current project is creating an HTML formatted unordered list ([list]) from any XML file. My current pseudo-code is as follows:

"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>--i.e., the depth is the same. If current element - previous element = 1, then we have increased in depth and we need another[list] and <li>. Conversely, if current element - previous element < 0, take that absolute difference and create that number of </ul> closing tags.

(I know there will need to be other closing tags and other tests, but this is a start. If there is a better/easier way of doing it, please stop me here.)

The first stumbling block I am working on now is that of getting the depths of the previous element. Here is my code:
Code:
<?xml version='1.0' encoding='utf-8'?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method='html' version='1.0' encoding='utf-8' indent='no'/>
<xsl:template match="/">

<xsl:param name="prevdepth" select="number"/>

<xsl:for-each select="//*">
<xsl:value-of select="local-name()"/>: <xsl:apply-templates select="current()/text()"/>
(depths: <xsl:value-of select="count(ancestor-or-self::*)"/>-<xsl:value-of select="$prevdepth"/>)
<xsl:call-template name="ulli"/>
</xsl:for-each>
</xsl:template>

<xsl:template name="ulli">
<xsl:param name="prevdepth" select="count(ancestor-or-self::*)"/>
</xsl:template>

</xsl:stylesheet>
Because I need the relative depth, I am first calling a template and storing the current depth as a parameter there. Then, I retrieve that parameter value on the next for-each iteration. Currently, I am getting an empty string for all of the difference values. I've tried various things, one of which gets me "0" instead of nothing...though I can't remember what the code was.

Thanks in advance for any help!