xsl-list
[Top] [All Lists]

RE: Dynamically controlling sort order based on values in the XML

2005-06-03 07:10:11

I need to be able to sort the data based on the CONFIG element of the XML
and I don't know ahead of time how many columns there are going to be in the
dataset and how many of them I will need to sort by.

I'd approach this as a multi-pass (in this case 2-pass) transform, as follows:

pass 1: write a stylesheet that, given your input xml, generates as its output another stylesheet that does what you want (sorts by multiple cols, displays multiple cols, etc).

pass 2: run this generated stylesheet against the original input

Regards,

--A

From: "Steve W" <lsl(_at_)btconnect(_dot_)com>
Reply-To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Subject: [xsl] Dynamically controlling sort order based on values in the XML
Date: Fri, 3 Jun 2005 10:27:37 +0100

I have some xml that looks like :

<DISPLAY>
    <CONFIG>
        <COL>
            <ID>ColA</ID>
            <ORDERBY DIRECTION="ASC">1</ORDERBY>
        </COL>
        <COL>
            <ID>ColB</ID>
            <ORDERBY DIRECTION="DSC">2</ORDERBY>
        </COL>
    </CONFIG>
    <DATA>
        <ROW>
            <COL ID="ColA">
                <VALUE>BBB</VALUE>
            </COL>
            <COL ID="ColB">
                <VALUE>111</VALUE>
            </COL>
        </ROW>
        <ROW>
            <COL ID="ColA">
                <VALUE>CCC</VALUE>
            </COL>
            <COL ID="ColB">
                <VALUE>333</VALUE>
            </COL>
        </ROW>
        <ROW>
            <COL ID="ColA">
                <VALUE>AAA</VALUE>
            </COL>
            <COL ID="ColB">
                <VALUE>222</VALUE>
            </COL>
        </ROW>
    </DATA>
</DISPLAY>

I have a stylesheet that outputs the data sorted by the element in CONFIG
where COL/ORDERBY/text() is 1,  which looks like :

<xsl:template match="DISPLAY">
   <xsl:variable name="sort_col"
select="CONFIG/COL[ORDERBY/text()='1']/ID/text()" />
   <xsl:apply-templates select="DATA/ROW" mode="do_data">
      <xsl:sort select="COL[(_at_)ID=$sort_col]/VALUE/text()" />
   </xsl:apply-templates>
</xsl:template>

<xsl:template match="ROW" mode="do_data">
   <xsl:value-of select="COL[(_at_)ID='ColA']/VALUE/text()" />, <xsl:value-of
select="COL[(_at_)ID='ColB']/VALUE/text()" />
   <br />
</xsl:template>

This gives me :
AAA, 222
BBB, 111
CCC, 333

I need to be able to sort the data based on the CONFIG element of the XML
and I don't know ahead of time how many columns there are going to be in the
dataset and how many of them I will need to sort by.

Can anyone suggest a way to achive this ?

_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar ? get it now! http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>