Thursday, 15 September 2011

xml - Summing related values -



xml - Summing related values -

i have .xml

<?xml version="1.0" encoding="iso-8859-1"?> <output> <orders> <order> <id>1</id> <number>10002</number> <type>loading</type> <date>2013-01-01t02:30:00</date> </order> <order> <id>2</id> <number>10003</number> <type>loading</type> <date>2013-01-01t010:30:00</date> </order> <order> <id>3</id> <number>10004</number> <type>loaded</type> <date>2013-01-01t12:30:00</date> </order> </orders> <quantities> <quantity> <id_order>1</id_order> <unit>kg</unit> <value>1000</value> </quantity> <quantity> <id_order>1</id_order> <unit>pal</unit> <value>3</value> </quantity> <quantity> <id_order>1</id_order> <unit>m3</unit> <value>1.5</value> </quantity> <quantity> <id_order>2</id_order> <unit>kg</unit> <value>2000</value> </quantity> <quantity> <id_order>2</id_order> <unit>pal</unit> <value>4</value> </quantity> <quantity> <id_order>3</id_order> <unit>kg</unit> <value>5000</value> </quantity> </quantities> </output>

and desired transformation like

<output> <amount>kg</amount> <total> "sum of kg elements" </total> <amount>pal</amount> <total> "sum of pal elements" </total> <amount>m3</amount> <total> "sum of m3 elements" </total> </output>

i've tried

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:template match="/"> <output> <xsl:for-each select="output/quantities/quantity"> <amount> <xsl:value-of select="unit"/> <xsl:for-each select="output/quantities/quantity/value"> <xsl:value-of select="sum(output/quantities/quantity/value)"/> </xsl:for-each> </amount> </xsl:for-each> </output> </xsl:template> </xsl:stylesheet>

but don't sum of values , output broken this

<output> <amount>kg<total/></amount> <amount>pal<total/></amount> <amount>m3<total/></amount> <amount>kg<total/></amount> <amount>pal<total/></amount> <amount>kg<total/></amount> </output>

i've tried few other workarounds none worked desired output. if suggest or help in way appreciate it.

now im trying add together node, in first xml, called after should have value of copied value stripped show after hours (hh:mm:ss). i've managed create re-create of original xml , add together tag, i'm not sure on how extract hr info , show in within tags.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="date"> <xsl:copy-of select="."/> <hour> <xsl:comment> <xsl:copy-of select="substring(output/orders/order/date,11)"/> </xsl:comment> </hour> </xsl:template> </xsl:stylesheet>

try way:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:key name="qty" match="quantity" use="unit"/> <xsl:template match="/"> <output> <amount>kg</amount> <total><xsl:value-of select="sum(key('qty', 'kg')/value)"/></total> <amount>pal</amount> <total><xsl:value-of select="sum(key('qty', 'pal')/value)"/></total> <amount>m3</amount> <total><xsl:value-of select="sum(key('qty', 'm3')/value)"/></total> </output> </xsl:template> </xsl:stylesheet>

note not best output format choose; ideally, unit , amount contained mutual element, not adjacent siblings.

xml xslt xslt-1.0

No comments:

Post a Comment