Q: Out of order document

Discussion in 'XML' started by G. Ralph Kuntz, MD, MS, Feb 14, 2008.

  1. How do people deal with out-of-order document? Let me clarify.

    Let's say we have an input document having the following format:

    <document>
    section 1
    section 2
    section 3
    </document>

    we want the resulting output document to have the structure

    <document>
    section 3
    section 2
    section 1
    </document>

    Since the xsl template matching will match section 1, then section 2,
    etc. how do we get the output into the format we want?

    Do you use param(s) to store intermediate values?

    Thanks, Ralph
     
    G. Ralph Kuntz, MD, MS, Feb 14, 2008
    #1
    1. Advertisements

  2. G. Ralph Kuntz, MD, MS schrieb:
    > How do people deal with out-of-order document? Let me clarify.
    >
    > Let's say we have an input document having the following format:
    >
    > <document>
    > section 1
    > section 2
    > section 3
    > </document>
    >
    > we want the resulting output document to have the structure
    >
    > <document>
    > section 3
    > section 2
    > section 1
    > </document>
    >
    > Since the xsl template matching will match section 1, then section 2,
    > etc. how do we get the output into the format we want?


    xsl:sort?
    --
    Johannes Koch
    In te domine speravi; non confundar in aeternum.
    (Te Deum, 4th cent.)
     
    Johannes Koch, Feb 14, 2008
    #2
    1. Advertisements

  3. G. Ralph Kuntz, MD, MS wrote:
    > How do people deal with out-of-order document? Let me clarify.
    >
    > Let's say we have an input document having the following format:
    >
    > <document>
    > section 1
    > section 2
    > section 3
    > </document>
    >
    > we want the resulting output document to have the structure
    >
    > <document>
    > section 3
    > section 2
    > section 1
    > </document>
    >
    > Since the xsl template matching will match section 1, then section 2,
    > etc. how do we get the output into the format we want?


    Here is an example, assuming the document element has section child
    elements like this

    <document>
    <section>section 1</section>
    <section>section 2</section>
    <section>section 3</section>
    </document>

    then you can process them and sort in descending order

    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:eek:utput method="xml" indent="yes"/>

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

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

    </xsl:stylesheet>

    to reverse the original order.



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Feb 14, 2008
    #3
  4. Do you really mean the "section" lines to be raw text, as you've shown
    them? If so, you need to do that with string processing, which is
    possible but somewhat awkward in XSLT.

    If they have XML structure, then as others have said you can use
    sorting... or, if you know the expected sections in advance, you can
    simply have the template for <document> explicitly process them in a
    specific order by using more specific apply-template selections.

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Feb 14, 2008
    #4
  5. I meant xml sub-sections. I found the answer. I did not know that
    <xsl:apply-templates> could take an argument.

    Thanks.
     
    G. Ralph Kuntz, MD, MS, Feb 14, 2008
    #5
    1. Advertisements

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. bugbear
    Replies:
    4
    Views:
    2,082
    bugbear
    Jan 28, 2004
  2. Soren Kuula
    Replies:
    2
    Views:
    816
    Soren Kuula
    Feb 1, 2004
  3. Phil
    Replies:
    3
    Views:
    722
    Janwillem Borleffs
    May 27, 2004
  4. Phlip
    Replies:
    8
    Views:
    1,188
    Phlip
    Mar 18, 2009
  5. cspoh
    Replies:
    0
    Views:
    431
    cspoh
    Jul 31, 2003
  6. Stephan Kämper
    Replies:
    2
    Views:
    394
    Stephan Kämper
    Jan 18, 2004
  7. Trigger0219

    document jscript function call order...

    Trigger0219, Aug 12, 2007, in forum: Javascript
    Replies:
    2
    Views:
    208
    David Mark
    Aug 12, 2007
  8. ofir
    Replies:
    0
    Views:
    298
Loading...