473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Traversion order cf. output order in XSL

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
2 1539

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Tanguy Fautré | last post by:
Hello, does std::multimap make any guarantee about the insertion order? for example: int main() { std::multimap<int, int> Map;
7
by: Dave | last post by:
#include <iostream> using namespace std; struct foo { int a; int b; };
27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
1
by: Jeff Blee | last post by:
I hope someone can help me get this graph outputing in proper order. After help from Tom, I got a graph to display output from the previous 12 months and include the average of that output all in...
10
by: aatish19 | last post by:
1: Write a program that asks the user to input an integer value and print it in a reverse order Sample Output Enter any number 65564 Reverse of 65564 is 46556 2: Write a program that takes a...
16
by: mdh | last post by:
May I ask the group the following: (Again, alas , from K&R) This is part of a function: while ( ( array1 = array2 ) != '\0' ); /* etc etc */ Is this the order that this is evaluated? ...
3
by: Zongjun Qi | last post by:
Hey, In the book <Effective C++>, the author provides an example to prove why we need "pass by reference". I redoed the example, and found something interesting. The codes are:...
1
by: 28490 | last post by:
Hi, In 10.2 when a query is run with GROUP BY it does not order the output by the grouped columns. In earlier versions the output was always sorted by the the grouped columns even though there...
10
by: somenath | last post by:
Hi All, Please help me to understand the reason behind the output of the following program What is the output of the following program: int i=10; int f1() { static int i = 15; printf("f1:%d...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.