我试图在XSLT 1.0样式表中保持一个计算值的累计值(我的收入的10%,四舍五入到最接近的10美分),但是舍入错误给我带来了不准确的结果。我知道我当前的代码是错误的,但是我不知道如何纠正它。
我用这个尝试了总和:
<xsl:value-of select="format-number(ceiling(sum(./bsk:Date/@income) div 10), '$#.00')" />
这引入了我提到的舍入错误。
我已经尝试使用以下方法求和:
<xsl:value-of select="format-number(sum(ceiling(./bsk:Date/@income div 10)), '$#.00')" />
<xsl:value-of select="format-number(sum(ceiling(./bsk:Date/@income div 10)), '$#.00')" /></td>
我收到错误:“ XSLT转换期间出错:预期XPath表达式将返回NodeSet。”
下面是我正在使用的代码。
<!-- The XML: -->
<Month name="June">
<Date num="28" day="Friday">
<Expense amount="62.50" for="Business License" />
</Date>
<Date num="29" day="Saturday" income="61.30" tithe="paid" />
<Date num="30" day="Sunday" income="108.45" />
</Month>
<!-- The XSLT -->
<xsl:for-each select="./bsk:Month"><table>
<tbody>
<xsl:for-each select="./bsk:Date">
<xsl:variable name="income"><xsl:choose>
<xsl:when test="@income"><xsl:value-of select="@income" /></xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose></xsl:variable>
<xsl:variable name="tithe" select="ceiling($income) div 10" />
<xsl:variable name="expenses"><xsl:choose>
<xsl:when test="./bsk:Expense"><xsl:value-of select="sum(./bsk:Expense/@amount)" /></xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose></xsl:variable>
<xsl:variable name="rowspan"><xsl:choose>
<xsl:when test="count(./bsk:Expense) > 1"><xsl:value-of select="count(./bsk:Expense)" /></xsl:when>
<xsl:otherwise><xsl:value-of select="1" /></xsl:otherwise>
</xsl:choose></xsl:variable>
<tr>
<td>
<xsl:attribute name="rowspan"><xsl:value-of select="$rowspan" /></xsl:attribute>
<xsl:choose>
<xsl:when test="@income">
<xsl:text>$</xsl:text>
<xsl:value-of select="$income" />
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="class">null</xsl:attribute>
<xsl:text> - </xsl:text>
</xsl:otherwise>
</xsl:choose>
</td>
<td>
<xsl:attribute name="rowspan"><xsl:value-of select="$rowspan" /></xsl:attribute>
<xsl:choose>
<xsl:when test="@income">
<xsl:if test="@tithe='paid'"><xsl:attribute name="class">tithe_paid</xsl:attribute></xsl:if>
<xsl:value-of select="format-number($tithe, '$#.00')" />
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="class">null</xsl:attribute>
<xsl:text> - </xsl:text>
</xsl:otherwise>
</xsl:choose>
</td>
<xsl:choose>
<xsl:when test="./bsk:Expense">
<td><xsl:value-of select="./bsk:Expense/@for" /></td>
<td><xsl:value-of select="format-number(./bsk:Expense[1]/@amount, '$#.00')" /></td>
</xsl:when>
<xsl:otherwise><td colspan="2" class="null"> - </td></xsl:otherwise>
</xsl:choose>
<td>
<xsl:attribute name="rowspan"><xsl:value-of select="$rowspan" /></xsl:attribute>
<xsl:if test="($income - $expenses) < 0"><xsl:attribute name="class">hole</xsl:attribute></xsl:if>
<xsl:value-of select="format-number($income - $expenses - $tithe, '$#.00')" />
</td>
</tr>
<xsl:for-each select="./bsk:Expense[position() > 1]">
<tr>
<td><xsl:value-of select="@for" /></td>
<td><xsl:value-of select="format-number(@amount, '$#.00')" /></td>
</tr>
</xsl:for-each>
</xsl:for-each>
</tbody>
<tfoot><tr>
<th>Totals</th>
<td headers="Income"><xsl:value-of select="format-number(sum(./bsk:Date/@income), '$#.00')" /></td>
<td headers="Tithe"><xsl:value-of select="format-number(ceiling(sum(./bsk:Date/@income) div 10), '$#.00')" /></td>
<td colspan="2" headers="Exp_Amount"><xsl:value-of select="format-number(sum(./bsk:Date/bsk:Expense/@amount), '$#.00')" /></td>
<td headers="Exp_Net">
<xsl:if test="(sum(./bsk:Date/@income) - sum(./bsk:Date/bsk:Expense/@amount)) < 0"><xsl:attribute name="class">hole</xsl:attribute></xsl:if>
<xsl:value-of select="format-number(sum(./bsk:Date/@income) - sum(./bsk:Date/bsk:Expense/@amount), '$#.00')" />
</td>
</tr></tfoot>
</table></xsl:for-each>
6月的十分之一的价格为$ 6.20和$ 10.90,这合计为$ 17.10。但由于我当月的总收入为169.75,因此将其四舍五入为$ 17.00。因此,我的本应为90.15美元的净值显示为90.25美元,太高了10美分。
答案 0 :(得分:0)
请考虑以下简化示例:
XML
<input>
<entry date="2019-07-01" amount="61.30"/>
<entry date="2019-07-02" amount="108.45"/>
</input>
XSLT 1.0(+ EXSLT节点集())
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- first pass -->
<xsl:template match="/input" >
<!-- first pass -->
<xsl:variable name="pre-process-entries">
<xsl:for-each select="entry">
<entry date="{@date}" amount="{@amount}" tithe="{ceiling(@amount) div 10}"/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="entries" select="exsl:node-set($pre-process-entries)/entry"/>
<!-- output -->
<table border="1">
<tr>
<th>Date</th>
<th>Amount</th>
<th>Tithe</th>
</tr>
<xsl:for-each select="$entries">
<tr>
<td>
<xsl:value-of select="@date"/>
</td>
<td>
<xsl:value-of select="format-number(@amount, '$#.00')"/>
</td>
<td>
<xsl:value-of select="format-number(@tithe, '$#.00')"/>
</td>
</tr>
</xsl:for-each>
<tr>
<th>TOTAL</th>
<th>
<xsl:value-of select="format-number(sum($entries/@amount), '$#.00')"/>
</th>
<th>
<xsl:value-of select="format-number(sum($entries/@tithe), '$#.00')"/>
</th>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="utf-8"?>
<table border="1">
<tr>
<th>Date</th>
<th>Amount</th>
<th>Tithe</th>
</tr>
<tr>
<td>2019-07-01</td>
<td>$61.30</td>
<td>$6.20</td>
</tr>
<tr>
<td>2019-07-02</td>
<td>$108.45</td>
<td>$10.90</td>
</tr>
<tr>
<th>TOTAL</th>
<th>$169.75</th>
<th>$17.10</th>
</tr>
</table>
已渲染