Re: [xsl] Dynamically controlling sort order based on values in the XML

Subject: Re: [xsl] Dynamically controlling sort order based on values in the XML
From: omprakash.v@xxxxxxxxxxxxx
Date: Fri, 3 Jun 2005 17:30:37 +0530
Hi,

         Check out my comments below:



<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">

                              <!-- VVVVVVVVVVVVVVVVVV Remove the
/VALUE/text() from here  -->
      <xsl:sort select="COL[@ID=$sort_col]/VALUE/text()" />
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
     <xsl:sort select="COL[@ID=$sort_col2]/VALUE/text()" />

<!-- You are sorting on nodes not on their content. If you want to sort by
more than 1 col you can specify one more sort instruction as above. -->

   </xsl:apply-templates>


</xsl:template>

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

<!-- You just list the columns you want displayed above -->

   <br />
</xsl:template>

Hope this helps.

Cheers,
Prakash






                                                                                                                   
                    "Steve W"                                                                                      
                    <lsl@btconnec        To:     <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>                                 
                    t.com>               cc:     (bcc: omprakash.v/Polaris)                                        
                                         Subject:     [xsl] Dynamically controlling sort order based on values in  
                    06/03/2005           the XML                                                                   
                    02:57 PM                                                                                       
                    Please                                                                                         
                    respond to                                                                                     
                    xsl-list                                                                                       
                                                                                                                   
                                                                                                                   




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[@ID=$sort_col]/VALUE/text()" />
   </xsl:apply-templates>
</xsl:template>

<xsl:template match="ROW" mode="do_data">
   <xsl:value-of select="COL[@ID='ColA']/VALUE/text()" />, <xsl:value-of
select="COL[@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 ?

Thanks

Steve






This e-Mail may contain proprietary and confidential information and is sent for the intended recipient(s) only. 
If by an addressing or transmission error this mail has been misdirected to you, you are requested to delete this mail immediately.
You are also hereby notified that any use, any form of reproduction, dissemination, copying, disclosure, modification,
distribution and/or publication of this e-mail message, contents or its attachment other than by its intended recipient/s is strictly prohibited.

Visit Us at http://www.polaris.co.in

Current Thread