Forum Moderators: open
I'm working with the xml output of an application which, unfortunately, is not hierarchical, though it arguably describes a hierarchy. Nevertheless, I would like to xslt it to a series of nested unordered lists.
The (simplified) xml looks like this:
<item>
<position>1</position>
<name>Top Level</name>
</item>
<item>
<position>2</position>
<name>Second Level</name>
</item>
<item>
<position>1</position>
<name>Top Level</name>
</item>
<item>
<position>2</position>
<name>Second Level</name>
</item>
<item>
<position>3</position>
<name>Third Level</name>
</item>
<item>
<position>3</position>
<name>Third Level</name>
</item>
<item>
<position>2</position>
<name>Second Level</name>
</item>
<item>
<position>1</position>
<name>Top Level</name>
</item>
...and I would like to translate it to this:
<ul>
<li>Top Level
<ul>
<li>Second Level</li>
</ul>
</li>
<li>Top Level
<ul>
<li>Second Level
<ul>
<li>Third Level</li>
<li>Third Level</li>
<li>Third Level</li>
</ul>
</li>
<li>Second Level</li>
</ul>
</li>
</ul>
I've tried a couple of approaches, but the main problem is getting the opening and closing <ul> into the <li> of a parent item.
The best idea I thought I had was to use an
if to test if the current item's <position> was greater than that of it's preceding-sibling, and then output the desired value like this: <ul><li><xsl:value-of select="name"/></li> ...and do a similar job where the current item's <position> was less than the
preceding-sibling's <position>. Unfortunately, this stops the parser since it is invalid... Can anyone suggest an approach?
-B