sorting <xsl:for-each> versus <xsl:apply-templates>

Discussion in 'XML' started by Sparko, Jul 9, 2004.

  1. Sparko

    Sparko Guest

    My Xml is as follows;


    <root>
    <st id="1">
    <rt id="1">
    <cr id="1" ca_w="" ca_x="" ca_y="" ca_z="" other attribs>
    <ct some attribs>
    <te some attribs />
    <te some attribs />
    <te some attribs />
    <te some attribs />
    </ct>
    <cr id="1" ca_w="" ca_x="" ca_y="" ca_z="" other attribs>
    <ct some attribs>
    <te some attribs />
    <te some attribs />
    <te some attribs />
    <te some attribs />
    </ct>
    </cr>
    <cr id="1" ca_w="" ca_x="" ca_y="" ca_z="" other attribs>
    <ct some attribs>
    <te some attribs />
    <te some attribs />
    <te some attribs />
    <te some attribs />
    </ct>
    </cr>
    </rt>
    <rt id="2">
    <cr id="1" ca_a="" ca_b="" ca_c="" ca_d="" other attribs>
    <ct some attribs>
    <te some attribs />
    <te some attribs />
    <te some attribs />
    <te some attribs />
    </ct>
    <cr id="1" ca_a="" ca_b="" ca_c="" ca_d="" other attribs>
    <ct some attribs>
    <te some attribs />
    <te some attribs />
    <te some attribs />
    <te some attribs />
    </ct>
    </cr>
    <cr id="1" ca_a="" ca_b="" ca_c="" ca_d="" other attribs>
    <ct some attribs>
    <te some attribs />
    <te some attribs />
    <te some attribs />
    <te some attribs />
    </ct>
    </cr>
    </rt>
    </st>




    I have written a recursive xslt template to iterate down the nodes and
    produce a "tree" like output. I have used the xsl axis [child::*],
    calling the template again within a for-each loop. I want to achieve
    sorting on the "cr" nodes' ca_ attributes and as can be seen the
    attributes change depending upon the st/rt xml sections. Therefore the
    sort criteria needs to change from one section of the xml to another.
    I'm not too sure this can be achieved due to the way I am processing
    the document. I tried to use a sort key such as below, which didn't
    work.

    st[@id=1]/rt[@id=1]/cr/@ca_x

    When just specifying @ca_x or @ca_y, when the xslt processor reaches
    the first set of cr nodes it sorts it correctly, the only problem
    being that I have no way of changing the sort criteria when it reaches
    the next st/rt section. I have a feeling I should perhaps be
    processing each st/rt section using the apply-templates element
    therefore allowing me to specify different sort criteria for each. I
    am not too sure how the apply-templates works if you want to process
    the whole of a dynamic xml document, i.e if I'm not sure at processing
    time how many st/rt sections there will be, the for-each seems the
    only way I can process everything? I would be grateful for any advice.

    Regards,

    Sparko.
     
    Sparko, Jul 9, 2004
    #1
    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. Stefan Siegl
    Replies:
    1
    Views:
    964
    Marrow
    Jul 18, 2003
  2. Cyrille \cns\ Szymanski

    xsl apply-templates select=not(...)

    Cyrille \cns\ Szymanski, Nov 7, 2003, in forum: XML
    Replies:
    4
    Views:
    597
    Cyrille \cns\ Szymanski
    Nov 11, 2003
  3. Vijay singh
    Replies:
    1
    Views:
    436
    Martin Honnen
    Nov 4, 2004
  4. KJ
    Replies:
    7
    Views:
    579
    Andreas Kraftl
    May 29, 2005
  5. Paul Butcher
    Replies:
    12
    Views:
    730
    Gary Wright
    Nov 28, 2007
Loading...

Share This Page