XSLT sorting / merging / summing

D

david.paik

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
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top