简体   繁体   English

为什么总和功能在XSLT1.0中不起作用?

[英]why the sum fucntion can not work in XSLT1.0?

I use 'sum function' to calculate the total quantity, the XML structure is a little complex, I searched online, sum function can be used in XSLT 1.0, but I tried it, it does not work for me, I do not know why? 我使用'sum函数'来计算总量,XML结构有点复杂,我在网上搜索,sum函数可以在XSLT 1.0中使用,但我试过了,它对我不起作用,我不知道为什么?

my XSLT: 我的XSLT:

  <xsl:template match="/">
  <html>
<body>
<table border="1">
      <tr>
      <th>Quantity</th>
       </tr>
       <xsl:for-each select="warehouses/warehouse">
       <tr>      
          <td><xsl:value-of select="sum(items/item/s_qty)"/></td>     
       </tr>
      </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

my XML : 我的XML:

<warehouses>
    <warehouse>
             <w_id>22</w_id>
             <w_name>Namekagon</w_name>
             <address>
                    <w_city>Indonesia</w_city>
                    <w_country>Indonesia</w_country>
             </address>
             <items>
                   <item>
                        <i_name>MECLIZINE HYDROCHLORIDE</i_name>
                        <s_qty>909</s_qty>
                    </item>
                </items>
      </warehouse>
      <warehouse>
             <w_id>21</w_id>
             <w_name>kagon</w_name>
             <address>
                    <w_city>Indonesia</w_city>
                    <w_country>Indonesia</w_country>
             </address>
             <items>
                   <item>
                        <i_name>MECLIZINE HYDROCHLORIDE</i_name>
                        <s_qty>587</s_qty>
                    </item>
                </items>
      </warehouse>
      <warehouse>
             <w_id>21</w_id>
             <w_name>kagon</w_name>
             <address>
                    <w_city>Singapore</w_city>
                    <w_country>Singapore</w_country>
             </address>
             <items>
                   <item>
                        <i_name>MECLIZINE HYDROCHLORIDE</i_name>
                        <s_qty>587</s_qty>
                    </item>
                </items>
      </warehouse>

the result should be: 1496 结果应该是:1496

if you want sum for all s_qty, you have to change 如果你想要所有s_qty的总和,你必须改变

  <xsl:for-each select="warehouses">
       <tr>      
          <td><xsl:value-of select="sum(warehouse/items/item/s_qty)"/></td>     
       </tr>
   </xsl:for-each>

see transformation at https://xsltfiddle.liberty-development.net/bnnZW7 请参阅https://xsltfiddle.liberty-development.net/bnnZW7上的转型

also you can use <xsl:value-of select="sum(//s_qty)"/> 你也可以使用<xsl:value-of select="sum(//s_qty)"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM