"Soren Kuula" <> wrote in message
news:_0PSb.81740$ k...
> Hi,
>
> I'm trying to teach myself a little XSL.
>
> I have made up an XML model of a consed list, like :
> <list>
> <car>a</car>
> <cdr>
> <list>
> <car>b</car>
> <cdr>
> <list>
> <car>c</car>
> <cdr>
> <list>
> <car>d</car>
> </list>
> </cdr>
> </list>
> </cdr>
> </list>
> </cdr>
> </list>
>
> (so, car is the value of a list element and cdr is the successor -
> Scheme nomenclature).
>
> Making xsl scripts that dumped the list (a,b,c,d) in forward and reverse
> order were not too difficult, and neither was dumping the first, second,
> second-from-last and last values in the list.
>
> Now I want to output a list in the format above, which is the reverse og
> the original list :
>
> <list>
> <car>d</car>
> <cdr>
> <list>
> <car>c</car>
> <cdr>
> <list>
> <car>b</car>
> <cdr>
> <list>
> <car>a</car>
> </list>
> </cdr>
> </list>
> </cdr>
> </list>
> </cdr>
> </list>
>
> One rough way to reverse it is:
> 1) Recursive descent to the last element
> 2) Output the car of that
> 3) Output the other car's on the way back from recursive descent, adding
> some "cdr" and "list"
> 4) output </list></cdr> as many times as the length of the list
>
> - but I need some good ideas where to begin with this in XSL ..
> (and I wonder if the solution will look like the obvious ML program for
> the same purpose).
One easy way to accomplish this in XSLT is the following.
This transformation:
<xsl:stylesheet version="1.0"
xmlns

sl="http://www.w3.org/1999/XSL/Transform">
<xsl

utput omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<list>
<xsl:apply-templates select="(//car)[position() = last()]"/>
</list>
</xsl:template>
<xsl:template match="car">
<xsl:copy-of select="."/>
<xsl:apply-templates select="../../.."/>
</xsl:template>
<xsl:template match="list">
<cdr>
<list>
<xsl:apply-templates select="car"/>
</list>
</cdr>
</xsl:template>
</xsl:stylesheet>
when applied on your source.xml:
<list>
<car>a</car>
<cdr>
<list>
<car>b</car>
<cdr>
<list>
<car>c</car>
<cdr>
<list>
<car>d</car>
</list>
</cdr>
</list>
</cdr>
</list>
</cdr>
</list>
produces the wanted result:
<list>
<car>d</car>
<cdr>
<list>
<car>c</car>
<cdr>
<list>
<car>b</car>
<cdr>
<list>
<car>a</car>
</list>
</cdr>
</list>
</cdr>
</list>
</cdr>
</list>
Hope this helped.
Cheers,
Dimitre Novatchev [XML MVP]
FXSL developer, XML Insider,
http://fxsl.sourceforge.net/ -- the home of FXSL
Resume:
http://fxsl.sf.net/DNovatchev/Resume/Res.html