Traversion order cf. output order in XSL

S

Soren Kuula

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).

Soren
 
D

Dimitre Novatchev -- MVP

Soren Kuula said:
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:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek: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
 
S

Soren Kuula

Hi, Dimitre,
One easy way to accomplish this in XSLT is the following.

This transformation:

<xsl:stylesheet version="1.0" ...
Hope this helped.

Cheers,

Dimitre Novatchev [XML MVP]
FXSL developer, XML Insider,

It sure did ! And I see it even works in singleton and empty lists.

Thanks a lot

Soren
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top