0

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 to generate sorted list of invoices in HTML format. I am able to do that with help 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 two questions

1. How to SUM?

I need to display sum of all invoices in the last row of the table. I have an idea that I need to use nodeset but I am not able to figure out how?

2. Dynamic Sort

Is it possible to provide element name or attribute name dynamically to xsl:sort.

For example the attribute name on which to sort is provided as different element value.

<sortby>Gross_spcAmount</sortby>
4

1 回答 1

0

(1) 如何求和:使用内置的 XPath 库。

sum(/PropertySet/*/FS_spcInvoice)

如果存在被处理的任何数字实际上不是数字的风险,则存在细微差别,在这种情况下,总和会因包含错误值而被破坏。这可以通过以下方式防止:

sum(/PropertySet/*/FS_spcInvoice[number(.)=number(.)])

...这依赖于NaNthat的属性NaN != NaN

(2) 动态排序:它是可能的,虽然不优雅......您必须使用构成您正在查找的节点的地址从您需要的任何变量值或地址来表达排序字符串的计算。除了公开属性节点的名称并检查它之外,没有真正的解决方法。

<xsl:sort select="@*[name(.)=/*/sortby]"/>
于 2013-08-12T23:17:52.387 回答