XSLT - Sum based on attribute values -


i have following source xml

source xml

<?xml version="1.0" encoding="utf-16"?> <propertyset><siebelmessage><listofxrx_spcusco_spccol_spcinvoice_spcar_spcsummary> <fs_spcinvoice  type_spccode="20"  xcs_spcinq_spcpo_spcnumber="7500020052"   xcs_spcinq_spcserial_spcnumber="vdr551061" xcs_spcinq_spccustomer_spcnumber="712246305" xcs_spcinq_spcinvoice_spcnumber="060853967" gross_spcamount="747.06" invoice_spcdate="04/01/2012"></fs_spcinvoice> <fs_spcinvoice type_spccode="20"  xcs_spcinq_spcpo_spcnumber="7500020052"   xcs_spcinq_spcserial_spcnumber="vdr551061" xcs_spcinq_spccustomer_spcnumber="712346305" xcs_spcinq_spcinvoice_spcnumber="063853967" gross_spcamount="947.06" invoice_spcdate="04/01/2013"></fs_spcinvoice> </listofxrx_spcusco_spccol_spcinvoice_spcar_spcsummary></siebelmessage></propertyset> 

i need generate sorted list of invoices in html format. able following xslt

xslt

<?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" > <xsl:template match="/" > <html><head></head><body> <h3>summary</h3>     <table> <thead> <tr><th>invoice number</th><th>customer number</th><th>serial number</th><th>po number</th><th>invoice date</th><th>invoice amount</th></tr> </thead><tbody> <xsl:apply-templates select="propertyset/siebelmessage/listofxrx_spcusco_spccol_spcinvoice_spcar_spcsummary/fs_spcinvoice"> <xsl:sort select="@gross_spcamount" order="descending" /> </xsl:apply-templates> <tr><td colspan="5">total amount</td><td></td></tr> </tbody></table></body></html> </xsl:template> <xsl:template match='fs_spcinvoice'> <tr>     <td><xsl:value-of select="@xcs_spcinq_spcinvoice_spcnumber" /></td>     <td><xsl:value-of select="@xcs_spcinq_spccustomer_spcnumber" /></td>     <td><xsl:value-of select="@xcs_spcinq_spcserial_spcnumber" /></td>     <td><xsl:value-of select="@xcs_spcinq_spcpo_spcnumber" /></td>     <td><xsl:value-of select="@invoice_spcdate" /></td>     <td><xsl:value-of select="@gross_spcamount" /></td>  </tr> </xsl:template> </xsl:stylesheet> 

i have 2 questions

1. how sum?

i need display sum of invoices in last row of table. have idea need use nodeset not able figure out how?

2. dynamic sort

is possible provide element name or attribute name dynamically xsl:sort.

for example attribute name on sort provided different element value.

<sortby>gross_spcamount</sortby> 

(1) how sum: use built-in xpath library.

sum(/propertyset/*/fs_spcinvoice) 

there nuance if there risk of numbers being addressed not numbers, in case sum spoiled including bad value. prevented by:

sum(/propertyset/*/fs_spcinvoice[number(.)=number(.)]) 

... relies on property of nan nan != nan.

(2) dynamic sort: possible, though inelegant ... have express calculation of sort string using address composes node looking whatever variable value or address need. no real way around other exposing name of attribute node , checking it.

<xsl:sort select="@*[name(.)=/*/sortby]"/> 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -