Xslt 汇总所有产品及其各自订购数量的总和XSL
当前获取XSLT中的NaN错误。 我想获得所有产品及其订购数量的总价值或收入 XML: 我试着适应递归模板 XSL:Xslt 汇总所有产品及其各自订购数量的总和XSL,xslt,xpath,Xslt,Xpath,当前获取XSLT中的NaN错误。 我想获得所有产品及其订购数量的总价值或收入 XML: 我试着适应递归模板 XSL: 期望的结果是Total=26您的递归模板看起来不错,我认为问题在于您的初始pName1和pName2参数值。试一试 <xsl:with-param name="pName1" select="'productamount'"/> <xsl:with-param name="pName2" select="'productcost'"/> 您的模板希望这些
期望的结果是Total=26您的递归模板看起来不错,我认为问题在于您的初始pName1和pName2参数值。试一试
<xsl:with-param name="pName1" select="'productamount'"/>
<xsl:with-param name="pName2" select="'productcost'"/>
您的模板希望这些参数是包含相关元素名称的字符串,这就是为什么您需要在双引号select属性中使用单引号的原因。我发现XSLT存在一些问题。首先,pNodes参数的设置不正确
<xsl:with-param name="pNodes" select="customers/customer/invoices/invoice/products"/>
从XML来看,它需要如下所示
<xsl:with-param name="pNodes" select="customers/invoices/invoice/products/product"/>
i、 e.发票元素不在样本中的customer元素中,此外,您希望对单个产品元素求和
<xsl:with-param name="pName1" select="'productamount'"/>
<xsl:with-param name="pName2" select="'productcost'"/>
接下来,正如Ian Roberts提到的,pName1和pName2看起来应该设置为元素名。目前,您只是将它们设置为这些元素第一次出现时的值
<xsl:with-param name="pName1" select="'productamount'"/>
<xsl:with-param name="pName2" select="'productcost'"/>
最后,pAccum参数的设置是错误的。您当前正在执行此操作
<xsl:with-param name="pAccum"
select="$pAccum + $pNodes[1]/*[name()=$pName1] * pNodes[1]/*[name()=$pName2]"/>
但是,在第二个pNodes变量之前,您遗漏了$。应该是这样的:
<xsl:with-param name="pAccum"
select="$pAccum + $pNodes[1]/*[name()=$pName1] * $pNodes[1]/*[name()=$pName2]"/>
这是完整的XSLT,它将为您提供26个
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:call-template name="sumProducts">
<xsl:with-param name="pNodes" select="customers/invoices/invoice/products/product"/>
<xsl:with-param name="pName1" select="'productamount'"/>
<xsl:with-param name="pName2" select="'productcost'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="sumProducts">
<xsl:param name="pNodes"/>
<xsl:param name="pName1"/>
<xsl:param name="pName2"/>
<xsl:param name="pAccum" select="0"/>
<xsl:choose>
<xsl:when test="not($pNodes)">
<xsl:value-of select="$pAccum"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="sumProducts">
<xsl:with-param name="pNodes" select="$pNodes[position() > 1]"/>
<xsl:with-param name="pName1" select="$pName1"/>
<xsl:with-param name="pName2" select="$pName2"/>
<xsl:with-param name="pAccum" select="$pAccum + $pNodes[1]/*[name()=$pName1] * $pNodes[1]/*[name()=$pName2]"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
嗯…试过了,还是有点不对劲。许多递归模板只针对一个客户或发票……但不是一个完整的模板:解决这种情况的唯一方法是,您需要阅读并学习XSLT——您遇到的问题是,即使您得到了正确的解决方案,您也不理解它们,无法使用它们。@DimitreNovatchev我正在努力学习,先生。这些是每个人都使用的通用模板,很可能是您自己开发的。我现在正在学习,这就是我来这里的原因。我要求的是指针而不是答案…第二步检查代码,客户元素被包装在发票和产品元素周围,虽然pAccum在这里缺少$a,但我的代码不是->打字错误。仍然没有合计产品价格和它们的…金额。这很可能是XPaTH问题吗…?->谢谢你的帮助,伙计!
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:call-template name="sumProducts">
<xsl:with-param name="pNodes" select="customers/invoices/invoice/products/product"/>
<xsl:with-param name="pName1" select="'productamount'"/>
<xsl:with-param name="pName2" select="'productcost'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="sumProducts">
<xsl:param name="pNodes"/>
<xsl:param name="pName1"/>
<xsl:param name="pName2"/>
<xsl:param name="pAccum" select="0"/>
<xsl:choose>
<xsl:when test="not($pNodes)">
<xsl:value-of select="$pAccum"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="sumProducts">
<xsl:with-param name="pNodes" select="$pNodes[position() > 1]"/>
<xsl:with-param name="pName1" select="$pName1"/>
<xsl:with-param name="pName2" select="$pName2"/>
<xsl:with-param name="pAccum" select="$pAccum + $pNodes[1]/*[name()=$pName1] * $pNodes[1]/*[name()=$pName2]"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>