sum values by lookup

Discussion in 'XML' started by Rolf Kemper, Dec 23, 2004.

  1. Rolf Kemper

    Rolf Kemper Guest

    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 ############################
     
    Rolf Kemper, Dec 23, 2004
    #1
    1. Advertising

  2. Rolf Kemper wrote:

    > <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="gSizes" select="$gCells/@Size" />

    > <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
     
    Luke Dalessandro, Dec 23, 2004
    #2
    1. Advertising

  3. Luke Dalessandro wrote:
    > 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
     
    Luke Dalessandro, Dec 23, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Petterson Mikael
    Replies:
    0
    Views:
    424
    Petterson Mikael
    Aug 31, 2005
  2. The alMIGHTY N
    Replies:
    1
    Views:
    627
    Joris Gillis
    Feb 9, 2007
  3. Juho Jussila

    Xslt sum of absolute values

    Juho Jussila, Nov 12, 2007, in forum: XML
    Replies:
    4
    Views:
    1,653
    Juho Jussila
    Nov 13, 2007
  4. Atif Jalal

    Sum of a column values in a Datagrid

    Atif Jalal, Jan 21, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    434
    Saravana [MVP]
    Jan 28, 2004
  5. Filiz Duman

    SUM TEXTFIELD VALUES

    Filiz Duman, Jan 12, 2004, in forum: Javascript
    Replies:
    1
    Views:
    98
    Randy Webb
    Jan 12, 2004
Loading...

Share This Page