## [xsl] How to gain efficience using Muenchian method sorted on a computed value

 Subject: [xsl] How to gain efficience using Muenchian method sorted on a computed value From: "Yang" Date: Fri, 1 Jun 2001 17:18:02 +0800
```Hi,  Everyone,  and Jeni,

Jeni,
Thanks for your patience with me for these days to provide  many seful
suggestions.
In the last mail, you quote as :
1. >I'm afraid that I'm at a
>bit of a loss when it comes to how you should be using the XML
>declaration at this point, as the XML string that you're creating is a
>JavaScript string, and the encoding that you declare in the XML
>declaration within it shouldn't (I think) make much of a difference

You are absolute right this point. Since then I have tested it  with xml
with Chinese
character, no problem at all. There is nothing to do with XML declaration.
Anyhow it gives me a chance to practice the XML serialization.

2.  I have learned a lot from the xsl-lists, especially from your reply,
the unique set of data using key element, Muenchian method, and key
function
for the given group data.
Now working on the real problems, I have  to display the unique set
value which
are sorted  on the computed values for example, sum(amount),
sum(unitPrice * quantity).
If using a general approach such as
<xsl:for-each  select="....>
<xsl:sort select="sum(amount)"/>
<xsl:value-of select="sum(amount)"/>..</xsl:for-each>
it takes at least twice computation effort to get the output.

So My question is this : will the following two step approach gain some
efficiency
what are the possible disadvantages included?

3.  Two step approach outlined as:
1.   Process the Muenchian function and save the unique set data
as well as sum(amount) in xml structure using RTF.
2.   2.1 Using RTF as param to bring back the unique set data and
computed value
and sum(amount) when needed.
2.2  While displaying the output, display this unique set value on
the value
of sum(amount) and other related data.
2.3  When using key function to get the related data of each piece
of unique data,
the original document to derive unique data has to be recalled.
Such as:

<xsl:for-each select="msxsl:node-set(\$tempResult)/line" >
<xsl:sort select="Amount" order="descending" data-type="number"/>

<xsl:variable name="thisPP" select="concat(Account, ':', Date)"/>
<tr>
<td><xsl:value-of select="Account"/></td>
<td><xsl:value-of select="Date"/></td>
<td><xsl:value-of select="Amount"/></td>
<!-- recalling original set -->
<xsl:for-each select="/">
<xsl:for-each select="key('lines',\$thisPP)">
<td><xsl:value-of select="Amount"/></td>
</xsl:for-each>
</xsl:for-each>
</tr>
</xsl:for-each>

Will this recalling process and RTF offset some gain in computation
saving?
Why do use <for-each select="/"> and define \$thisPP for recalling
process?
I have puzzled quite a while , even I keep on get desired solutions.

** xsl**  listing below from the xsl-list is revised for illustration.

Sun-fu Yang

sfyang@xxxxxxxxxxxxx

**xsl**
<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:msxsl="urn:schemas-microsoft-com:xslt" >
<xsl:output method="html" indent="yes"/>

<xsl:key name="lines"
match="line"
use="concat(Account, ':', Date)" />
<xsl:variable name="originalDoc" select="/"/>
<xsl:template match="/">
<html>
<!--  get the unique set of value account,date and amount  and save in RTF
**tempResult** -->
<xsl:variable name="tempResult">
<xsl:apply-templates
select="//line[count(.|key('lines', concat(Account, ':', Date)))=
1]" />
</xsl:variable>

<!--  display the solution from recalling RTF -->
<table>
<tr><th>account</th><th>date</th><th>amount</th></tr>
<xsl:for-each select="msxsl:node-set(\$tempResult)/line" >
<xsl:sort select="Amount" order="descending" data-type="number"/>

<xsl:variable name="thisPP" select="concat(Account, ':', Date)"/>
<tr>
<td><xsl:value-of select="Account"/></td>
<td><xsl:value-of select="Date"/></td>
<td><xsl:value-of select="Amount"/></td>
<xsl:for-each select="\$originalDoc">
<xsl:for-each select="key('lines',\$thisPP)">
<td><xsl:value-of select="Amount"/></td>
</xsl:for-each>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>

</html>
</xsl:template>

<xsl:template match="line" >
<line>
<xsl:copy-of select="Account | Date" />
<Amount>
<xsl:value-of select="sum(key('lines', concat(Account, ':',
Date))/Amount)" />
</Amount>
</line>
</xsl:template>
</xsl:stylesheet>

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

```