0

当前在 XSLT 中出现 NaN 错误。我想获得所有产品及其订购数量的总价值或收入。

XML:

<customers>
<customer>
<customername>John</customername>
</customer>
<invoices>
 <invoice>
  <invoicenumber>01</invoicenumber>
  <products>
   <product>
    <productname>candy bar</productname>
    <productamount>6</productamount
    <productcost>2</productcost>
   </product>
  </products>
 </invoice>
 <invoice>
  <invoicenumber>02</invoicenumber>
  <products>
   <product>
    <productname>choco bar</productname>
    <productamount>3</productamount
    <productcost>1</productcost>
   </product>
  </products>
 </invoice>
</invoices>
<customer>
<customername>Fritz</customername>
</customer>
<invoices>
 <invoice>
  <invoicenumber>01</invoicenumber>
  <products>
   <product>
    <productname>Soda</productname>
    <productamount>3</productamount
    <productcost>2</productcost>
   </product>
  </products>
 </invoice>
 <invoice>
  <invoicenumber>02</invoicenumber>
  <products>
   <product>
    <productname>lollipop</productname>
    <productamount>5</productamount
    <productcost>1</productcost>
   </product>
  </products>
 </invoice>
</invoices>

而且我试图适应递归模板。

XSL:

Total:  
<xsl:call-template name="sumProducts">
<xsl:with-param name="pNodes" select="customers/customer/invoices/invoice/products"/>
<xsl:with-param name="pName1" select="././././././productamount"/>
<xsl:with-param name="pName2" select="././././././productcost"/>
</xsl:call-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>

期望的结果是 Total = 26

4

2 回答 2

2

我可以看到您的 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"/>

invoices元素不在样本中的customer元素中,此外,您还希望将各个产品元素相加。

接下来,正如 Ian Roberts 所提到的,pName1pName2看起来应该设置为元素名称。目前,您只是将它们设置为这些元素第一次出现的值。

<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() &gt; 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>
于 2012-10-22T12:09:33.113 回答
0

您的递归模板看起来不错,我认为问题出在您的初始值pName1pName2参数值上。尝试

<xsl:with-param name="pName1" select="'productamount'"/>
<xsl:with-param name="pName2" select="'productcost'"/>

您的模板期望这些参数是包含相关元素名称的字符串,这就是为什么您需要在双引号select属性中使用单引号的原因。

于 2012-10-22T11:49:00.267 回答