[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Re: Sort & Max simultaneous operations


Subject: [xsl] Re: Sort & Max simultaneous operations
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Sat, 30 Aug 2003 10:58:55 +0200

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:key name="kRowC1" match="Row" use="@C1"/>

  <xsl:template match="/">
    <xsl:for-each
     select="/*/*/Row[generate-id()
                     =
                      generate-id(key('kRowC1',
                                       @C1
                                       )[1]
                                  )
                     ]">

      <xsl:variable name="vAllInGroup" select="key('kRowC1', @C1)"/>
      <xsl:copy-of select="$vAllInGroup
                             [not(@C2 &lt; $vAllInGroup/@C2)]"/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

when applied on your source.xml:

<PickList>
  <TotalRows> 33</TotalRows>
  <Columns C1="Deposit" C2="Period" C3="Status" C4="Value Date" C5="Maturity
Date" C6="Deposit No"/>
  <Tags> ctlMskDepositNo *</Tags>
  <Rows>
    <Row C1="1" C2="1" C3="Closed " C4="23/10/2003" C5="23/04/2004" C6="1"/>
    <Row C1="1" C2="2" C3="Closed " C4="23/10/2003" C5="23/04/2004" C6="2"/>
    <Row C1="1" C2="3" C3="Closed " C4="23/10/2003" C5="23/04/2004" C6="3"/>
    <Row C1="1" C2="4" C3="Closed " C4="23/10/2003" C5="23/04/2004" C6="4"/>
    <Row C1="1" C2="5" C3="Matured " C4="23/10/2003" C5="23/04/2004"
C6="5"/>
    <Row C1="1" C2="6" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="6"/>
    <Row C1="2" C2="1" C3="Closed" C4="23/10/2003" C5="23/04/2004" C6="7"/>
    <Row C1="2" C2="2" C3="Closed" C4="23/10/2003" C5="23/04/2004" C6="8"/>
    <Row C1="2" C2="3" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="9"/>
    <Row C1="2" C2="3" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="10"/>
    <Row C1="3" C2="1" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="11"/>
    <Row C1="4" C2="1" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="12"/>
    <Row C1="5" C2="1" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="13"/>
    <Row C1="5" C2="8" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="13"/>
    <Row C1="5" C2="6" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="11"/>
    <Row C1="5" C2="7" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="12"/>
  </Rows>
</PickList>

produces the wanted result:

<Row C1="1" C2="6" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="6" />
<Row C1="2" C2="3" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="9" />
<Row C1="2" C2="3" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="10" />
<Row C1="3" C2="1" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="11" />
<Row C1="4" C2="1" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="12" />
<Row C1="5" C2="8" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="13" />

It should be noted that the way all nodes with a maximum value in a group
are found is O(N^2) and would be very slow if there are groups with big
number of elements.

In this case an O(N) (recursive) or O(N*log(N)) (using sort) algorithm must
be used.

One can also call the "maximum" template from FXSL, instead of trying to
produce a new maximum template on every occasion.



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL


<aruniima.chakrabarti@xxxxxxxxxxxxxxxxxx> wrote in message
news:CBF6DBC01C62C64DA820DCFCD48E05C802CD4298@xxxxxxxxxxxxxxxxxxxxxxxx
> Hi All,
> I have a xml  which has the structure below. I need to pick the rows with
attribute distinct C1 & the corresponding max attribute of C2 ...  What is
the best way to the same ?
>
> Input xml :
>
> <PickList>
>     <TotalRows>33</TotalRows>
>     <Columns C1="Deposit" C2="Period" C3="Status" C4="Value Date"
C5="Maturity Date" C6="Deposit No"/>
>     <Tags>ctlMskDepositNo *</Tags>
>     <Rows>
>         <Row C1="1" C2="1" C3="Closed " C4="23/10/2003" C5="23/04/2004"
C6="1"/>
>         <Row C1="1" C2="2" C3="Closed " C4="23/10/2003" C5="23/04/2004"
C6="2"/>
>         <Row C1="1" C2="3" C3="Closed " C4="23/10/2003" C5="23/04/2004"
C6="3"/>
>         <Row C1="1" C2="4" C3="Closed " C4="23/10/2003" C5="23/04/2004"
C6="4"/>
>         <Row C1="1" C2="5" C3="Matured " C4="23/10/2003" C5="23/04/2004"
C6="5"/>
>         <Row C1="1" C2="6" C3="Open" C4="23/10/2003" C5="23/04/2004"
C6="6"/>
>
>         <Row C1="2" C2="1" C3="Closed" C4="23/10/2003" C5="23/04/2004"
C6="7"/>
>         <Row C1="2" C2="2" C3="Closed" C4="23/10/2003" C5="23/04/2004"
C6="8"/>
>         <Row C1="2" C2="3" C3="Open" C4="23/10/2003" C5="23/04/2004"
C6="9"/>
>         <Row C1="2" C2="3" C3="Open" C4="23/10/2003" C5="23/04/2004"
C6="10"/>
>         <Row C1="3" C2="1" C3="Open" C4="23/10/2003" C5="23/04/2004"
C6="11"/>
>         <Row C1="4" C2="1" C3="Open" C4="23/10/2003" C5="23/04/2004"
C6="12"/>
>         <Row C1="5" C2="1" C3="Open" C4="23/10/2003" C5="23/04/2004"
C6="13"/>
>
>         <Row C1="5" C2="8" C3="Open" C4="23/10/2003" C5="23/04/2004"
C6="13"/>
>         <Row C1="5" C2="6" C3="Open" C4="23/10/2003" C5="23/04/2004"
C6="11"/>
>         <Row C1="5" C2="7" C3="Open" C4="23/10/2003" C5="23/04/2004"
C6="12"/>
>     </Rows>
> </PickList>
>
>
> the reqd output:
>
> <Rows>
> <Row C1="1" C2="6" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="6"/>
> <Row C1="2" C2="3" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="9"/>
> <Row C1="2" C2="3" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="10"/>
> <Row C1="3" C2="1" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="11"/>
> <Row C1="4" C2="1" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="12"/>
> <Row C1="5" C2="8" C3="Open" C4="23/10/2003" C5="23/04/2004" C6="12"/>
> </Rows>
>
> Thank you,
>
> Regards,
> aruniima
>
>
>
>
>
> DISCLAIMER:
> This message contains privileged and confidential information and is
intended only for the individual named.If you are not the intended recipient
you should not disseminate,distribute,store,print, copy or deliver this
message.Please notify the sender immediately by e-mail if you have received
this e-mail by mistake and delete this e-mail from your system.E-mail
transmission cannot be guaranteed to be secure or error-free as information
could be intercepted,corrupted,lost,destroyed,arrive late or incomplete or
contain viruses.The sender therefore does not accept liability for any
errors or omissions in the contents of this message which arise as a result
of e-mail transmission. If verification is required please request a
hard-copy version.
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>
>




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread
Keywords
xml