sum values by lookup

R

Rolf Kemper

Dear Experts,

right on time before Xmas I have something to think about.
I want to sum up values by using a key. But the value which is used as
a selctor in the key function is a node-set.

I made a smal test program and run it with 3 pocessors.
ALTOVA (XMLSPY 2005) , MSXML4 , SAXON 8.

The results and messages differ !
ALTOVA( = 1300 (which was my expectation)
MSXML4 = 600 (which is may be right, but not was was expecting)
SAXON8 = 600 ( with additional warning )

1) What is correct way to make my dream come true ?
2) Why does SAXON complain (I'm using XSLT version 1.0 )

Plaese find below a test case

Thanks for your inputs and marry Christmas
Rolf

################# xml data ##################################
<?xml version="1.0" encoding="UTF-8"?>
<SumTest>
<Cells>
<Cell Name="A" Size="100"/>
<Cell Name="B" Size="200"/>
<Cell Name="C" Size="300"/>
</Cells>
<Instancies>
<Instance InstName="I1" CellName="A" Index="1" /><!-- 100 -->
<Instance InstName="I2" CellName="B" Index="2" /><!-- 200 -->
<Instance InstName="I3" CellName="A" Index="3" /><!-- 100 -->
<Instance InstName="I4" CellName="A" Index="4" /><!-- 100 -->
<Instance InstName="I5" CellName="C" Index="5" /><!-- 300 -->
<Instance InstName="I6" CellName="C" Index="6" /><!-- 300 -->
<Instance InstName="I7" CellName="B" Index="7" /><!-- 200 -->
<Instance InstName="I8" CellName="B" Index="8" /><!-- unselected-->
<Instance InstName="I9" CellName="C" Index="9" /><!-- unselected-->
</Instancies>
<!-- 3*A = 300 + 2*B = 400 + 2*C = 600 should sum up to 1300 -->
<!-- altova = 1300 (which might be OK just by different
interpretation of key/sum ) -->
<!-- msxml4 = 600 ( which is A+B+C probably first occurence of node
is used as key valu probably ) -->
<!-- saxon8 = 600 ( which is A+B+C probably first occurence of
node is used as key valu probably )
WARNING on compare !! comparsion of a node-set to a boolean has
changed dsince XPATH 1.0 "
-->
</SumTest>


################## xslt #######################################
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="text" version="1.0" encoding="UTF-8"/>
<xsl:key name="kCells" match="/SumTest/Cells/Cell" use="@Name"/>
<xsl:variable name="gEndIndex"
select="/SumTest/Instancies/Instance[@InstName='I8']/@index"/>
<xsl:variable name="gInstancies"
select="/SumTest/Instancies/Instance[@Index &gt; 0 and @index &lt;
$gEndIndex ]"/>
<xsl:variable name="vSumSize" select="sum(
key('kCells',$gInstancies/@CellName)/@Size)"/>
<xsl:template match="/">
<xsl:text>selected node count =</xsl:text>
<xsl:value-of select="count($gInstancies)"/>
<xsl:text>
</xsl:text>
<xsl:text>SumSize=</xsl:text>
<xsl:value-of select="$vSumSize"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>

######################## END ############################
 
L

Luke Dalessandro

Rolf said:
<xsl:key name="kCells" match="/SumTest/Cells/Cell" use="@Name"/>
<xsl:variable name="gEndIndex"
select="/SumTest/Instancies/Instance[@InstName='I8']/@Index"/>
<xsl:variable name="gInstancies"
select="/SumTest/Instancies/Instance[@Index &gt; 0 and @Index &lt;
$gEndIndex ]"/>

<xsl:variable name="gNames" select="$gInstancies/@CellName" />
<xsl:variable name="gCells" select="key('kCells', $gNames)" />
<xsl:variable name="vSumSize" select="sum(
key('kCells',$gInstancies/@CellName)/@Size)"/>

Rolf, I am using ASP.NET 2.0 (not sure which MSXML parser it is). Your
key lookup is only returning unique instances of the Cell element. If
you split up your complex lookup as I have done above, and then output
the count of $gCells and play with your data file, and your $gInstances
boundary, you will see this behavior.

In the case you have set up, all three Cells appear as Instancies, so
you get all three (and only all three) as $gCells. If you construct a
situation where only "A" and "B" appear as Instancies, no matter how
many times they appear, your $gCells will only have the two, and your
sum will be 300.

I'm not sure how to make your functionality work, but this is why it isn't.

Luke
 
L

Luke Dalessandro

Luke said:
Rolf, I am using ASP.NET 2.0 (not sure which MSXML parser it is). Your
key lookup is only returning unique instances of the Cell element.

To clarify my point, according to the standard near
http://www.w3.org/TR/1999/REC-xslt-19991116#key, in the definition of
the function "key":

"When the second argument to the key function is of type node-set, then
the result is the union of the result of applying the key function to
the string value of each of the nodes in the argument node-set."

The union is not an accumulation. Each unique node found will only
appear once.

Luke
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top