首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于属性值的XSLT和

基于属性值的XSLT和
EN

Stack Overflow用户
提问于 2013-08-12 22:08:27
回答 1查看 1.7K关注 0票数 0

我有以下的源XML

源XML

代码语言:javascript
复制
<?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>

我需要生成HTML格式的发票排序列表。在XSLT后面的帮助下,我能够做到这一点。

XSLT

代码语言:javascript
复制
<?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>

我有两个问题

1.如何求和?

我需要在表的最后一行显示所有发票的总和。我有一个想法,我需要使用节点,但我无法弄清楚如何?

2.动态排序

是否可以动态地向xsl:sort提供元素名称或属性名称。

例如,要对其排序的属性名称作为不同的元素值提供。

代码语言:javascript
复制
<sortby>Gross_spcAmount</sortby>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-12 23:17:52

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

代码语言:javascript
复制
sum(/PropertySet/*/FS_spcInvoice)

如果存在一个风险,即所处理的任何数字实际上不是数字,则存在细微差别,在这种情况下,包含坏值将破坏总数。防止这种情况的原因是:

代码语言:javascript
复制
sum(/PropertySet/*/FS_spcInvoice[number(.)=number(.)])

..。它依赖于NaN的属性NaN != NaN

(2)动态排序:这是可能的,虽然不雅.您必须使用一个地址来表示排序字符串的计算,该地址从所需的变量值或地址中组成要查找的节点。除了公开属性节点的名称并检查它之外,没有其他真正的方法。

代码语言:javascript
复制
<xsl:sort select="@*[name(.)=/*/sortby]"/>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18197477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档