如何使用XSL更新参数值计算不同订单表单的总费用
订单总费用计算的XSL修正方案
问题背景
我有两类订单XML数据,需要计算总费用,但现有XSL无法正常运行,求正确实现方式:
第一类:orderform-A
XML结构:
<?xml version="1.0" encoding="UTF-8"?> <Results> <form-type>orderform-A</form-type> <data> <form-data> <field> <name>productid-1</name> <value>Yes</value> </field> <field> <name>productid-3</name> <value>Yes</value> </field> <field> <name>productid-4</name> <value>Yes</value> </field> </form-data> </data> </Results>
定价与规则:
- productid-1(5美元)
- productid-2(5美元)
- productid-3(5美元)
- productid-4(5美元)
优惠规则:若同时订购productid-4与productid-2或productid-3,则productid-4免费;表单仅列出已订购产品。
第二类:orderform-B
XML结构:
<?xml version="1.0" encoding="UTF-8"?> <Results> <form-type>orderform-B</form-type> <data> <form-data> <field> <name>productid-w</name> <value>Yes</value> </field> <field> <name>productid-w-qty</name> <value>1</value> </field> <field> <name>productid-x</name> <value>Yes</value> </field> <field> <name>productid-x-qty</name> <value>1</value> </field> <field> <name>productid-y</name> <value>Yes</value> </field> <field> <name>productid-y-qty</name> <value>1</value> </field> </form-data> </data> </Results>
定价规则:
- productid-w(20美元)
- productid-x(10美元)
- productid-y(20美元)
- productid-z(10美元)
现有错误XSL代码
<xsl:param name="form-type"> <xsl:param name="total">0</xsl:param> <xsl:template match="/"> <xsl:choose> <xsl:when test="$form-type = 'orderform-A'"> <xsl:call-template name="orderform-A-total"/> </xsl:when> <xsl:when test="$form-type = 'orderform-B'"> <xsl:call-template name="orderform-B-total"/> </xsl:when> </xsl:choose> </xsl:template> <xsl:template name="orderform-A-total"> <xsl:if test="count(//Results/data/form-data/field[name='productid-1']/value) > 0"> <xsl:value-of select="$total + 5" /> </xsl:if> <xsl:if test="count(//Results/data/form-data/field[name='productid-2']/value) > 0"> <xsl:value-of select="$total + 5" /> </xsl:if> <xsl:if test="count(//Results/data/form-data/field[name='productid-3']/value) > 0"> <xsl:value-of select="$total + 5" /> </xsl:if> <xsl:if test="(count(//Results/data/form-data/field[name='productid-4']/value > 0)"> <xsl:value-of select="$total + 5" /> </xsl:if> <xsl:value-of select="$total" /></value> </xsl:template> <xsl:template name="orderform-B-total"> // TO DO </xsl:template>
修正后的完整XSL代码
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:variable name="form-type" select="/Results/form-type"/> <xsl:choose> <xsl:when test="$form-type = 'orderform-A'"> <xsl:call-template name="orderform-A-total"/> </xsl:when> <xsl:when test="$form-type = 'orderform-B'"> <xsl:call-template name="orderform-B-total"/> </xsl:when> <xsl:otherwise> <xsl:text>未知表单类型</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- 计算orderform-A总费用 --> <xsl:template name="orderform-A-total"> <xsl:variable name="products" select="/Results/data/form-data/field"/> <xsl:variable name="base-total" select="count($products)*5"/> <xsl:variable name="has-product4" select="boolean($products[name='productid-4'])"/> <xsl:variable name="has-eligible-product" select="boolean($products[name='productid-2'] or $products[name='productid-3'])"/> <xsl:variable name="final-total" select="$base-total - (if ($has-product4 and $has-eligible-product) then 5 else 0)"/> <xsl:text>总费用:</xsl:text> <xsl:value-of select="$final-total"/> <xsl:text> 美元</xsl:text> </xsl:template> <!-- 计算orderform-B总费用 --> <xsl:template name="orderform-B-total"> <xsl:variable name="form-data" select="/Results/data/form-data"/> <xsl:variable name="w-cost" select="if ($form-data/field[name='productid-w']/value='Yes') then 20 * number($form-data/field[name='productid-w-qty']/value) else 0"/> <xsl:variable name="x-cost" select="if ($form-data/field[name='productid-x']/value='Yes') then 10 * number($form-data/field[name='productid-x-qty']/value) else 0"/> <xsl:variable name="y-cost" select="if ($form-data/field[name='productid-y']/value='Yes') then 20 * number($form-data/field[name='productid-y-qty']/value) else 0"/> <xsl:variable name="z-cost" select="if ($form-data/field[name='productid-z']/value='Yes') then 10 * number($form-data/field[name='productid-z-qty']/value) else 0"/> <xsl:variable name="final-total" select="$w-cost + $x-cost + $y-cost + $z-cost"/> <xsl:text>总费用:</xsl:text> <xsl:value-of select="$final-total"/> <xsl:text> 美元</xsl:text> </xsl:template> </xsl:stylesheet>
关键修正说明
- 参数优化:直接从XML的
<form-type>节点读取表单类型,无需外部传递参数,避免人为错误。 - orderform-A逻辑修复:
- 原代码中
$total为不可变变量,无法累加计算,改用变量统计基础总价。 - 修复了
productid-4条件判断的语法错误(括号不匹配)。 - 实现优惠规则:同时满足productid-4和productid-2/3时,减去5美元。
- 原代码中
- orderform-B逻辑补全:
- 针对每个产品,判断是否选中后计算
单价×数量的费用。 - 对所有产品费用求和得到总费用。
- 针对每个产品,判断是否选中后计算
- 语法清理:移除原代码中未闭合的
</value>标签,修正格式错误。
内容的提问来源于stack exchange,提问作者adam78
相关产品推荐
相关产品推荐

