Forum Moderators: open

Message Too Old, No Replies

flat xml -> nested list via xslt

(application's xml output can't be changed...)

         

bedlam

8:04 am on Oct 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hiya,

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

macrost

4:16 pm on Oct 19, 2004 (gmt 0)

10+ Year Member



bedlam,
Do you happen to have any code yet? I don't have an xsl/xml editor here at the office, so if you could post some code, I might be able to help ya out.

Mac