Transform XML using XSLT based on element position

Discussion in 'XML' started by Igor, Jul 20, 2003.

  1. Igor

    Igor Guest

    Is there any way to resort and xml document using xslt based on
    element position.
    For example if I have xml like this:
    <root>
    <element> 1st thing </element>
    <element> 2nd thing </element>
    <element> 3rd thing </element>
    </root>

    would it be possible using xslt only to reverse it into:
    <root>
    <element> 3rd thing </element>
    <element> 2nd thing </element>
    <element> 1st thing </element>
    </root>

    I also can't sort based on the alphabetical or numerical order of the
    actual information between the element tags, only on the position of
    the tags, ie I would like to reverse their order.
     
    Igor, Jul 20, 2003
    #1
    1. Advertising

  2. "Igor" <> wrote in message
    news:...
    > Is there any way to resort and xml document using xslt based on
    > element position.
    > For example if I have xml like this:
    > <root>
    > <element> 1st thing </element>
    > <element> 2nd thing </element>
    > <element> 3rd thing </element>
    > </root>
    >
    > would it be possible using xslt only to reverse it into:
    > <root>
    > <element> 3rd thing </element>
    > <element> 2nd thing </element>
    > <element> 1st thing </element>
    > </root>


    Yes, this is straightforward. 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:strip-space elements="*"/>

    <xsl:template match="node() |@*">
    <xsl:copy>
    <xsl:apply-templates select="node() |@*"/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="root">
    <xsl:copy>
    <xsl:apply-templates>
    <xsl:sort select="position()"
    order="descending"
    data-type="number"/>
    </xsl:apply-templates>
    </xsl:copy>
    </xsl:template>
    </xsl:stylesheet>

    when applied on your source.xml:

    <root>
    <element> 1st thing </element>
    <element> 2nd thing </element>
    <element> 3rd thing </element>
    </root>

    produces the wanted result:

    <root>
    <element> 3rd thing </element>
    <element> 2nd thing </element>
    <element> 1st thing </element>
    </root>




    >
    > I also can't sort based on the alphabetical or numerical order of the
    > actual information between the element tags, only on the position of
    > the tags, ie I would like to reverse their order.



    Sorry, but I cannot understand what you mean here.


    =====
    Cheers,

    Dimitre Novatchev.
    http://fxsl.sourceforge.net/ -- the home of FXSL
     
    Dimitre Novatchev, Jul 20, 2003
    #2
    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. Isambella  via DotNetMonster.com

    Transform XML string using XSLT file

    Isambella via DotNetMonster.com, Aug 1, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    9,769
    Isambella via DotNetMonster.com
    Aug 2, 2005
  2. Replies:
    0
    Views:
    521
  3. Replies:
    1
    Views:
    686
    Joris Gillis
    Feb 2, 2005
  4. adi
    Replies:
    1
    Views:
    31,651
    Joe Kesselman
    Jun 6, 2006
  5. Water Cooler v2

    Transform an XML file using XSLT

    Water Cooler v2, Aug 1, 2006, in forum: XML
    Replies:
    1
    Views:
    761
    Joe Kesselman
    Aug 2, 2006
Loading...

Share This Page