xsl-list
[Top] [All Lists]

Re: [xsl] Get to 2 max values and element names

2012-05-11 10:37:35
William,

you did not suggest how the different Modifier values will be specified, so I 
made a guess. The solution (created with famous »Kernow for Saxon«!) is not 
very clever as I did not want to check out what XSLT 1 really can do. I have 
commented out some debugging output and there are no node-set() function 
included, but it worked with Kernow fine.

Check it out,

- Michael

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
xmlns:xs="http://www.w3.org/2001/XMLSchema"; version="1.0" 
exclude-result-prefixes="xs">
        <xsl:output indent="yes" />

        <xsl:variable name="mods">
                <mod nr="6">
                        <Steve>1.2</Steve>
                        <Helen>0.65</Helen>
                        <Jack>0.89</Jack>
                        <Petra>1.32</Petra>
                </mod>
        </xsl:variable>

        <xsl:template match="/">
                <out>
                        <xsl:variable name="nr" select="/Data/Modifier" />
                        <xsl:variable name="mod">
                                <xsl:copy-of select="$mods/mod[@nr = $nr]" />
                        </xsl:variable>
                        <xsl:variable name="calc">
                                <xsl:for-each select="/Data/List/*[number(.) 
&gt; 0]">
                                        <xsl:variable name="name" 
select="name(.)" />
                                        <xsl:if test="$mod/mod/*[name(.) = 
$name]">
                                                <xsl:element name="{name(.)}">
                                                        <xsl:value-of 
select="number(.) * number($mod/mod/*[name(.) = $name])" />
                                                </xsl:element>
                                        </xsl:if>
                                </xsl:for-each>
                        </xsl:variable>
                        <xsl:variable name="sort">
                                <xsl:for-each select="$calc/*">
                                        <xsl:sort select="." data-type="number" 
order="descending" />
                                        <xsl:copy-of select="." />
                                </xsl:for-each>
                        </xsl:variable>
                        <!-- xsl:copy-of select="$mod"/ -->
                        <!-- xsl:copy-of select="$calc"/ -->
                        <!-- xsl:copy-of select="$sort"/ -->
                        <xsl:value-of select="concat('TopValue = ', $sort/*[1], 
'&#xA;TopName = ', name($sort/*[1]), '&#xA;PenultValue = ', $sort/*[2], 
'&#xA;PenultName = ', name($sort/*[2]))" />
                </out>
        </xsl:template>

</xsl:transform>

Am 11.05.2012 um 15:36 schrieb W Charlton:


I have a peculiar problem. We have to extract the top 2 (modified) values 
from some elements in a list and also get the element names. The xml is 
something like:

<Data>
  <Modifier>6</Modifier>
  <List>
      <Julian>123</Julian>
      <Steve>45654</Steve>
      <Helen>98412</Helen>
      <Jack>-50</Jack>
      <Ruth>981</Ruth>
      <Susan>8431</Susan>
      <Trevor>354</Trevor>
      <Petra>6257</Petra>
      <Harold>25632</Harold>
      <Mary>8695</Mary>
  </List>
</Data>

Note: We know that sometimes Jack’s (and only Jack’s) value is negative, in 
which case he is excluded from the calculation.
It is also possible that any of the numbers are NULL in which case they are 
also excluded or ignored.

I need to do the following:

Depending on the value of the Modifier (1-10) we have 4 variables. 
vSteveMod, vHelenMod, vJackMod and vPetraMod 
These vary depending on the value of the Modifier, there are 10 quartets of 
values.

We now need to calculate the modified values for Steve, Helen, Jack and Petra 
(these 4 are chosen specifically for this test) dictated by the current 
Modifier (6).
So say that when the Modifier = 6, the 4 Modifying vars are:
vSteveMod = 1.2
vHelenMod = 0.65
vJackMod = 0.89
vPetraMod = 1.32

We then multiply these *Mod vars by the related value, so:
vSteveResult = 1.2 * 45654 = 54784.8
vHelenResult = 0.65 * 98412 = 63967.8
vJackResult = 0.89 * -50 = EXCLUDED
vPetraResult = 1.32 * 6257 = 8259.24

Finally we need to extract 4 values:
The Top value and element name.
The next (penultimate) highest value and also the element name.

e.g. in this case:

TopValue = 63967.8
TopName = Helen
PenultValue = 54784.8
PenultName = Steve

All using XSLT1

William Charlton
The yMonda team
yMonda Limited
w: www.ymonda.net



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



--
_______________________________________________________________
Michael Müller-Hillebrand: Dokumentations-Technologie
Adobe Certified Expert, FrameMaker
Lösungen und Training, FrameScript, XML/XSL, Unicode
Blog: http://cap-studio.de/ - Tel. +49 (9131) 28747





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