By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,492 Members | 1,199 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,492 IT Pros & Developers. It's quick & easy.

Traversion order cf. output order in XSL

P: n/a
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
--
Fjern de 4 bogstaver i min mailadresse som er indsat for at hindre s...
Remove the 4 letter word meaning "junk mail" in my mail address.

Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Soren Kuula" <do**********@bitplanet.net> wrote in message
news:_0*********************@news000.worldonline.d 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:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output 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
Jul 20 '05 #2

P: n/a
Hi, Dimitre,

Dimitre Novatchev -- MVP wrote:
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

--
Fjern de 4 bogstaver i min mailadresse som er indsat for at hindre s...
Remove the 4 letter word meaning "junk mail" in my mail address.

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.