XSLT sorting / merging / summing

Discussion in 'XML' started by david.paik@huscointl.com, Sep 20, 2006.

  1. Guest

    Given the data:

    <?xml version="1.0" encoding="UTF-8"?>

    <?xml-stylesheet type="text/xsl" href="C:\junk\830new.xslt"?>

    <SC830>

    <SC830_3 bano="S67777" cuno="002620" year="2007" week="38"
    item="282-333" qty="10" PO1="PO242" PO2="PO343" PO3="" PO4=""
    PO5="PO555" PO6="" PO7=""/>

    <SC830_3 bano="S67777" cuno="002620" year="2007" week="38"
    item="282-333" qty="12" PO1="PO242" PO2="PO343" PO3="" PO4="PO444"
    PO5="" PO6="" PO7=""/>

    <SC830_3 bano="S67777" cuno="002620" year="2007" week="44"
    item="282-666" qty="20" PO1="PO242" PO2="PO343" PO3="" PO4="" PO5=""
    PO6="" PO7=""/>

    </SC830>

    I want the 1st and 2nd elements merged, and the qty summed.

    I got close:

    <?xml version="1.0" encoding="UTF-8"?>

    <xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fn="http://www.w3.org/2005/xpath-functions">

    <xsl:eek:utput method="text" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:key name="linekey" match="SC830_3"
    use="concat(@bano,'*',@cuno,'*',@year,'*',@week,'*',@item,'*')"/>

    <xsl:template match="SC830">

    <xsl:apply-templates select="SC830_3[count(.|key('linekey',
    concat(@bano,'*',@cuno,'*',@year,'*',@week,'*',@item,'*'))[1]) = 1]"/>

    </xsl:template>

    <xsl:template match="SC830_3">

    <xsl:copy-of
    select="concat(@bano,'*',@cuno,'*',@year,'*',@week,'*',@item,'*')" />

    <xsl:value-of select="sum(key('linekey',
    concat(@bano,'*',@cuno,'*',@year,'*',@week,'*',@item,'*'))/@qty)" />*

    <xsl:value-of select="key('linekey',
    concat(@bano,'*',@cuno,'*',@year,'*',@week,'*',@item,'*'))/@PO1[string-length(.)
    &gt; 0]"/>*

    <xsl:value-of select="key('linekey',
    concat(@bano,'*',@cuno,'*',@year,'*',@week,'*',@item,'*'))/@PO2[string-length(.)
    &gt; 0]" />*

    <xsl:value-of select="key('linekey',
    concat(@bano,'*',@cuno,'*',@year,'*',@week,'*',@item,'*'))/@PO3[string-length(.)
    &gt; 0]" />*

    <xsl:value-of select="key('linekey',
    concat(@bano,'*',@cuno,'*',@year,'*',@week,'*',@item,'*'))/@PO4[string-length(.)
    &gt; 0]" />*

    <xsl:value-of
    select="key('linekey',concat(@bano,'*',@cuno,'*',@year,'*',@week,'*',@item,'*'))/@PO5[string-length(.)
    &gt; 0]" />*

    <xsl:value-of select="key('linekey',
    concat(@bano,'*',@cuno,'*',@year,'*',@week,'*',@item,'*'))/@PO6[string-length(.)
    &gt; 0]" />*

    <xsl:value-of select="key('linekey',
    concat(@bano,'*',@cuno,'*',@year,'*',@week,'*',@item,'*'))/@PO7[string-length(.)
    &gt; 0]" />*

    <xsl:text>

    </xsl:text>

    </xsl:template>

    </xsl:stylesheet>



    This gives me:

    S67777*002620*2007*38*282-333*22*

    PO242 PO242*

    PO343 PO343*

    *

    PO444*

    PO555*

    *

    *


    S67777*002620*2007*44*282-666*20*

    PO242*

    PO343*

    *

    *

    *

    *

    *



    So the QTYs sum ok and I get the right amount of lines. But I still
    have an issue with the PO attributes. I get:

    PO242 PO242*

    PO343 PO343*

    and I just want:

    PO242*

    PO343*

    I tried putting a [1] in but that returns the same thing.

    IDEAS?

    I am sure there are some XML experts out there that can solve this
    delema!

    Thanks!

    DP
     
    , Sep 20, 2006
    #1
    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. Yannick Patois

    Merging two XML tree: xslt or dom ?

    Yannick Patois, Jul 20, 2004, in forum: XML
    Replies:
    1
    Views:
    905
    Uche Ogbuji
    Jul 23, 2004
  2. Replies:
    4
    Views:
    495
    Martin Honnen
    Nov 1, 2005
  3. Osiris

    sorting/merging in C

    Osiris, Jun 16, 2007, in forum: C Programming
    Replies:
    6
    Views:
    400
    Osiris
    Jun 17, 2007
  4. ramu
    Replies:
    2
    Views:
    354
    Dann Corbit
    Nov 16, 2009
  5. Askar Ka
    Replies:
    2
    Views:
    206
    Kresimir Bojcic
    May 1, 2011
Loading...

Share This Page