xslt - Combining Similar Lines XML -


i have xml record have repeating unique id's combine similar id's 1 record, concat reference fields , summing amount field.

the xml looks this:

<root> <row>     <f01>123456</f01>     <f02>abc company</f02>     <f03>0</f03>     <f04>47582736</f04>     <f05>151.12</f05> </row> <row>     <f01>123456</f01>     <f02>abc company</f02>     <f03>0</f03>     <f04>47643792</f04>     <f05>191.09</f05> </row> <row>     <f01>123456</f01>     <f02>abc company</f02>     <f03>0</f03>     <f04>47643793</f04>     <f05>95.32</f05> </row> <row>     <f01>223344</f01>     <f02>dk corp</f02>     <f03>0</f03>     <f04>36819319</f04>     <f05>138.87</f05> </row> <row>     <f01>223344</f01>     <f02>dk corp</f02>     <f03>0</f03>     <f04>36827362</f04>     <f05>9.98</f05> </row> <row>     <f01>223344</f01>     <f02>dk corp</f02>     <f03>0</f03>     <f04>36834497</f04>     <f05>79.87</f05> </row> <row>     <f01>113964</f01>     <f02>direct company</f02>     <f03>0</f03>     <f04>1771929</f04>     <f05>400.07</f05> </row> <row>     <f01>113964</f01>     <f02>direct company</f02>     <f03>0</f03>     <f04>1766940</f04>     <f05>111.52</f05> </row> <row>     <f01>113964</f01>     <f02>direct company</f02>     <f03>0</f03>     <f04>1810269</f04>     <f05>112.48</f05> </row> <row>     <f01>113964</f01>     <f02>direct company</f02>     <f03>0</f03>     <f04>1618234</f04>     <f05>60.76</f05> </row> <row>     <f01>113964</f01>     <f02>direct company</f02>     <f03>0</f03>     <f04>1771923</f04>     <f05>2829.19</f05> </row> 

i want make this:

<root> <row>     <f01>123456</f01>     <f02>abc company</f02>     <f03>437.53</f03>     <f04>47582736, 47643792, 47643793</f04>     <f05>151.12</f05> </row> <row>     <f01>223344</f01>     <f02>dk corp</f02>     <f03>228.72</f03>     <f04>36819319, 36827362, 36834497</f04>     <f05>138.87</f05> </row> <row>     <f01>113964</f01>     <f02>direct company</f02>     <f03>3514.02</f03>     <f04>1771929, 1766940, 1810269, 1618234, 1771923</f04>     <f05>400.07</f05> </row> 

i think may know how concat f04 don't know how sum f05 , put value in f03. f01 unique id should determine keep together.

when xslt:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0">   <xsl:output omit-xml-declaration="no" indent="yes"/>   <xsl:strip-space elements="*"/>    <xsl:key name="krowbyf02" match="row" use="f02"/>    <xsl:template match="node()|@*">     <xsl:copy>       <xsl:apply-templates select="node()|@*"/>     </xsl:copy>   </xsl:template>    <xsl:template match="/*">     <root>       <xsl:apply-templates         select="row[generate-id() = generate-id(key('krowbyf02', f02)[1])]"/>     </root>   </xsl:template>    <xsl:template match="f03">      <f03>        <xsl:value-of          select="sum(key('krowbyf02', preceding-sibling::f02)/f05)" />      </f03>   </xsl:template>    <xsl:template match="f04">     <f04>       <xsl:apply-templates         select="key('krowbyf02', preceding-sibling::f02)/f04/text()"/>     </f04>   </xsl:template>    <xsl:template match="f04/text()">     <xsl:if test="not(position() = 1)">, </xsl:if>     <xsl:value-of select="."/>   </xsl:template> </xsl:stylesheet> 

...is applied provided xml:

<?xml version="1.0" encoding="utf-8"?> <root>   <row>     <f01>123456</f01>     <f02>abc company</f02>     <f03>0</f03>     <f04>47582736</f04>     <f05>151.12</f05>   </row>   <row>     <f01>123456</f01>     <f02>abc company</f02>     <f03>0</f03>     <f04>47643792</f04>     <f05>191.09</f05>   </row>   <row>     <f01>123456</f01>     <f02>abc company</f02>     <f03>0</f03>     <f04>47643793</f04>     <f05>95.32</f05>   </row>   <row>     <f01>223344</f01>     <f02>dk corp</f02>     <f03>0</f03>     <f04>36819319</f04>     <f05>138.87</f05>   </row>   <row>     <f01>223344</f01>     <f02>dk corp</f02>     <f03>0</f03>     <f04>36827362</f04>     <f05>9.98</f05>   </row>   <row>     <f01>223344</f01>     <f02>dk corp</f02>     <f03>0</f03>     <f04>36834497</f04>     <f05>79.87</f05>   </row>   <row>     <f01>113964</f01>     <f02>direct company</f02>     <f03>0</f03>     <f04>1771929</f04>     <f05>400.07</f05>   </row>   <row>     <f01>113964</f01>     <f02>direct company</f02>     <f03>0</f03>     <f04>1766940</f04>     <f05>111.52</f05>   </row>   <row>     <f01>113964</f01>     <f02>direct company</f02>     <f03>0</f03>     <f04>1810269</f04>     <f05>112.48</f05>   </row>   <row>     <f01>113964</f01>     <f02>direct company</f02>     <f03>0</f03>     <f04>1618234</f04>     <f05>60.76</f05>   </row>   <row>     <f01>113964</f01>     <f02>direct company</f02>     <f03>0</f03>     <f04>1771923</f04>     <f05>2829.19</f05>   </row> </root> 

...the wanted result produced:

<root>   <row>     <f01>123456</f01>     <f02>abc company</f02>     <f03>437.53</f03>     <f04>47582736, 47643792, 47643793</f04>     <f05>151.12</f05>   </row>   <row>     <f01>223344</f01>     <f02>dk corp</f02>     <f03>228.72</f03>     <f04>36819319, 36827362, 36834497</f04>     <f05>138.87</f05>   </row>   <row>     <f01>113964</f01>     <f02>direct company</f02>     <f03>3514.02</f03>     <f04>1771929, 1766940, 1810269, 1618234, 1771923</f04>     <f05>400.07</f05>   </row> </root> 

this classic grouping problem that, in case of xslt 1.0, uses muenchian grouping.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -