Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php XSL-获取价格总和_Php_Xml_Xslt_Sum - Fatal编程技术网

Php XSL-获取价格总和

Php XSL-获取价格总和,php,xml,xslt,sum,Php,Xml,Xslt,Sum,我的xml结构如下: <PartsDetail> <Id>1481</Id> <Desc>test1</Desc> <GlobDesc>test2</GlobDesc> <Price Cur="UAH">+798.27</Price> </PartsDetail> <PartsDetail> <Id>0741</Id> <Desc&g

我的xml结构如下:

<PartsDetail> <Id>1481</Id> <Desc>test1</Desc> <GlobDesc>test2</GlobDesc> <Price Cur="UAH">+798.27</Price> </PartsDetail> 
<PartsDetail> <Id>0741</Id> <Desc>test2</Desc> <GlobDesc>test2</GlobDesc> <Price Cur="UAH">+399.14</Price> </PartsDetail> 
1481测试1测试2+798.27
0741试验2试验2+399.14
考虑到这一点,我用“价格”进行了一些转换(如399.14)

我将此用于转换:

<xsl:call-template name="showNumberWithThousands">
 <xsl:with-param name="value">
 <xsl:call-template name="parseNumber">
<xsl:with-param name="value" select="Price"/>
</xsl:call-template>
 </xsl:with-param>
 </xsl:call-template>

我现在还需要取一笔价格。 我试着用这个:

<xsl:value-of select="sum(//Data/Paint//PartsDetail/@Price)"/>

但结果是——楠

据我所知,在将价格发送到函数“sum”之前,我需要在“普通视图(无+和-)中转换价格”

致:@michael.hor257k

结构更复杂。 我使用你的解决方案,但它不起作用。看来我做错了什么

<xsl:template name="PaintSum"> 
<xsl:variable name="corrected-prices"> 
<xsl:for-each select="//CalcData/Paint/PaintDtl"> 
<price> <xsl:value-of select="translate(MatAmnt, '+', '')"/> </price> 
</xsl:for-each> 
</xsl:variable> 
<sum> <xsl:value-of select="sum(exsl:node-set($corrected-prices)/price)"/> </sum>
</xsl:template> 

当我使用
什么也没发生。类似地,进一步请求进入模板将停止工作

我正在尝试使用:

<xsl:variable name="corrected-prices">
        <xsl:for-each select="//CalcData/Paint//PaintDtl">
            <price>
                <xsl:value-of select="translate(MatAmnt, '+', '')"/>
            </price>
        </xsl:for-each>
    </xsl:variable>

并通过以下方式在文本中添加总和:

<xsl:value-of select="sum(exsl:node-set($corrected-prices)/price)"/>

但是输出文件崩溃了

美元修正价格包含“1086.65286.75”

我怎样才能把它变成一个总数?

据我所知,我需要将价格转换为“正常视图”(无+和 -)将其发送到函数“sum”之前

这或多或少是正确的(如果数字为负数,您不希望删除减号)。给定格式良好的输入,例如:

XML

<root>
  <PartsDetail>
    <Id>1481</Id>
    <Desc>test1</Desc>
    <GlobDesc>test2</GlobDesc>
    <Price Cur="UAH">+798.27</Price>
  </PartsDetail>
  <PartsDetail>
    <Id>0741</Id>
    <Desc>test2</Desc>
    <GlobDesc>test2</GlobDesc>
    <Price Cur="UAH">+399.14</Price>
  </PartsDetail>
</root>

1481
测试1
测试2
+798.27
0741
测试2
测试2
+399.14
以下样式表:

XSLT1.0

<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:template match="/root">
    <xsl:variable name="corrected-prices">
        <xsl:for-each select="PartsDetail">
            <price>
                <xsl:value-of select="translate(Price, '+', '')"/>
            </price>
        </xsl:for-each>
    </xsl:variable>
    <sum>
        <xsl:value-of select="sum(exsl:node-set($corrected-prices)/price)"/>
    </sum>
</xsl:template>

</xsl:stylesheet>

将返回:

<?xml version="1.0" encoding="UTF-8"?>
<sum>1197.41</sum>

1197.41

此转换不需要使用任何扩展函数,也不会生成中间树——事实上,如果XSLT处理器能够识别尾部递归,它将使用0个额外内存运行,并且适合流式处理:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
   <xsl:apply-templates select="(*/Price/text())[1]">
     <xsl:with-param name="pNodes" select="*/Price/text()"/>
   </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="text()" name="sum">
  <xsl:param name="pTotal" select="0"/>
  <xsl:param name="pNodes"/>
  <xsl:param name="pNode1" select="translate($pNodes[1],'+','')"/>

  <xsl:value-of select="substring($pTotal+$pNode1, 1 div not($pNodes[2]))"/>
  <xsl:apply-templates select="$pNodes[2][$pNode1]">
    <xsl:with-param name="pTotal" select="$pTotal+$pNode1"/>
    <xsl:with-param name="pNodes" select="$pNodes[position()>1]"/>
  </xsl:apply-templates>
 </xsl:template>
</xsl:stylesheet>

发布一些代码会有帮助,“结构更复杂。我使用你的解决方案,但它不起作用。”1。我看不出你的结构,所以我不能帮你2.您没有使用我的解决方案。结构有其他信息(但这无关紧要)。我尝试将你的模板添加到我的文件中并调用它。(将此信息放在正确的位置)。我将属性的名称改为correct(MatAmnt等)——在第一个示例中,我使用了假定名称,我给出了有关问题的附加信息好的问题,+1。我只是提供了一个解决方案,它不依赖于任何扩展函数,也不创建任何中间结果树(消耗内存)。事实上,如果XSLT处理器识别尾部递归,则此转换将在常量内存中运行。
<root>
  <PartsDetail>
    <Id>1481</Id>
    <Desc>test1</Desc>
    <GlobDesc>test2</GlobDesc>
    <Price Cur="UAH">+798.27</Price>
  </PartsDetail>
  <PartsDetail>
    <Id>0741</Id>
    <Desc>test2</Desc>
    <GlobDesc>test2</GlobDesc>
    <Price Cur="UAH">+399.14</Price>
  </PartsDetail>
</root>
1187.4099999999998