Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Traversion order cf. output order in XSL

Reply
Thread Tools

Traversion order cf. output order in XSL

 
 
Soren Kuula
Guest
Posts: n/a
 
      01-31-2004
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.

 
Reply With Quote
 
 
 
 
Dimitre Novatchev -- MVP
Guest
Posts: n/a
 
      01-31-2004

"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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput 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


 
Reply With Quote
 
 
 
 
Soren Kuula
Guest
Posts: n/a
 
      02-01-2004
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use output of XSL transformation as new XSL stylesheet barney.b@iname.com XML 0 01-16-2006 02:29 PM
xsl - ignore XSL:FO tags, just output them paul.rusu@gmail.com XML 0 12-21-2005 03:49 PM
XSL Question tp xsl:for-each and xsl:variable schaf@2wire.ch XML 1 05-27-2005 09:25 PM
XSL-1000: (Fatal Error) Error while parsing XSL file (org.apache.xerces.parsers.AbstractSAXParser$AttributesProxy) Kevin Flood Java 0 09-08-2004 02:11 PM
use xsl:element to output an "<xsl:template match=...> ? Ray Tayek XML 3 11-30-2003 10:14 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57