473,805 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1557

"Soren Kuula" <do**********@b itplanet.net> wrote in message
news:_0******** *************@n ews000.worldonl ine.dk...
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:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="ye s" 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:styleshe et 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
13465
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
1353
by: Dave | last post by:
#include <iostream> using namespace std; struct foo { int a; int b; };
27
11221
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
6178
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 the one graph. The output was in the order of the months, but after unioning with the averages SQL code, the order is lost. Below is the full sql code that is the data source for the graph: SELECT (Format(.,"mmm"" '""yy")) AS Month,...
10
5400
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 string as an input and print it in reverse order.(don't use bulit-in/library function). Sample Output Enter any string: i m Kashif
16
2459
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? -> array2 is assigned to array1 ....???? the reason being it is
3
1570
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: ############################## #include <iostream> class Student{ public:
1
3768
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 was no ORDER BY clause present. In order to get an ordered output,decided to add ORCDER BY clause explicitly. But gave ORA-00904: invalid column name error for following query.
10
1598
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 ", i);
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10609
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10366
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9185
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3845
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.