Traversion order cf. output order in XSL

Discussion in 'XML' started by Soren Kuula, Jan 31, 2004.

  1. Soren Kuula

    Soren Kuula Guest

    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.
     
    Soren Kuula, Jan 31, 2004
    #1
    1. Advertising

  2. "Soren Kuula" <> wrote in message
    news:_0PSb.81740$...
    > 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
     
    Dimitre Novatchev -- MVP, Jan 31, 2004
    #2
    1. Advertising

  3. Soren Kuula

    Soren Kuula Guest

    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.
     
    Soren Kuula, Feb 1, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Flood
    Replies:
    0
    Views:
    1,040
    Kevin Flood
    Sep 8, 2004
  2. Ray Tayek
    Replies:
    3
    Views:
    530
    Ray Tayek
    Nov 30, 2003
  3. Replies:
    1
    Views:
    3,651
    A. Bolmarcich
    May 27, 2005
  4. Replies:
    0
    Views:
    574
  5. Replies:
    0
    Views:
    548
Loading...

Share This Page