Dear Dimitre,
Thank you for ur input. This was the transformation that I was trying to arrive
at.
I have few more queries.
In this case an O(N) (recursive) or O(N*log(N)) (using sort) algorithm must
be used.
Could guide me a bit more in this direction?
One approach I used was to Sort the xml first & the use the output xml to find
the row which is the last for each distinct C1.
But, I was unable to do the same in 1 transformation & had to do it in 2
simultaneous transformation.
The below transformation did not work.
My questions are
1. Is there of any way doing sort & the distinct row search in one xsl
2. Which is a better way to achieve the final output, 2 separate
transformations ( in case one single is not possible ) or the one which
grouping based on generate-id() ?
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="numCol" select="'C1'"/>
<xsl:template match="Rows">
<temp>
<xsl:apply-templates select="Row">
<xsl:sort select="@C1" data-type="number" order="ascending"/>
<xsl:sort select="@C2" data-type="number" order="descending"/>
</xsl:apply-templates>
</temp>
</xsl:template>
<xsl:template match="Row">
<xsl:if test="not(@*[name()=$numCol]=following::Row/@*[name()=$numCol])">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Thank you,
Regards,
aruniima
Regards,
aruniima
-----Original Message-----
From: Dimitre Novatchev [mailto:dnovatchev(_at_)yahoo(_dot_)com]
Sent: Saturday, August 30, 2003 2:29 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Re: Sort & Max simultaneous operations
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 < $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(_dot_)chakrabarti(_at_)iflexsolutions(_dot_)com> wrote in message
news:CBF6DBC01C62C64DA820DCFCD48E05C802CD4298(_at_)fmg-nt(_dot_)spz(_dot_)i-flex(_dot_)com(_dot_)(_dot_)(_dot_)
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
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