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. Advertising

  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. Advertising

  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. 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. Soren Kuula
    Replies:
    2
    Views:
    497
    Soren Kuula
    Feb 1, 2004
  2. Phil
    Replies:
    3
    Views:
    565
    Janwillem Borleffs
    May 27, 2004
  3. Phlip
    Replies:
    8
    Views:
    1,012
    Phlip
    Mar 18, 2009
  4. cspoh
    Replies:
    0
    Views:
    245
    cspoh
    Jul 31, 2003
  5. Stephan Kämper
    Replies:
    2
    Views:
    232
    Stephan Kämper
    Jan 18, 2004
Loading...

Share This Page