【问题标题】:在 Munechian 分组中需要帮助来计算每个变量的输出
【发布时间】:2022-01-14 15:21:21
【问题描述】:

请帮助我在添加变量值时遇到错误的求和逻辑。

下面的XML

<OutboundPaymentInstruction>
    <OutboundPayment>
        <DocumentPayable>
            <DocumentPayableLine>
                <LineNumber>2</LineNumber>
                <PONumber/>
                <ConsumptionAdviceNumber/>
                <LineType>
                    <Code>TAX</Code>
                    <Meaning/>
                </LineType>
                <LineDescription>Utilities; Justification: W-IN-18-6402-1237845 </LineDescription>
                <LineGrossAmount>
                    <Value>7912.5</Value>
                    <Currency>
                        <Code>THB</Code>
                        <NumericCode>764</NumericCode>
                    </Currency>
                </LineGrossAmount>
                <UnitPrice>0</UnitPrice>
                <Quantity/>
                <UnitOfMeasure>
                    <Code/>
                    <Meaning/>
                </UnitOfMeasure>
                <Tax>
                    <TaxCode>TH VAT</TaxCode>
                    <TaxRate>7</TaxRate>
                    <TaxRateCode>TH AP STD SVC 7</TaxRateCode>
                </Tax>
            </DocumentPayableLine>
            <DocumentPayableLine>
                <LineNumber>3</LineNumber>
                <PONumber/>
                <ConsumptionAdviceNumber/>
                <LineType>
                    <Code>AWT</Code>
                    <Meaning/>
                </LineType>
                <LineDescription>Utilities; Justification: W-IN-18-6402-1237845 </LineDescription>
                <LineGrossAmount>
                    <Value>-3391.07</Value>
                    <Currency>
                        <Code>THB</Code>
                        <NumericCode>764</NumericCode>
                    </Currency>
                </LineGrossAmount>
                <UnitPrice>0</UnitPrice>
                <Quantity/>
                <UnitOfMeasure>
                    <Code/>
                    <Meaning/>
                </UnitOfMeasure>
                <Tax>
                    <TaxCode>TH WHT</TaxCode>
                    <TaxRate>2.999999</TaxRate>
                    <TaxRateCode>TH WHT SVCS 03</TaxRateCode>
                </Tax>
            </DocumentPayableLine>
            <DocumentPayableLine>
                <LineNumber>1</LineNumber>
                <PONumber/>
                <ConsumptionAdviceNumber/>
                <LineType>
                    <Code>ITEM</Code>
                    <Meaning/>
                </LineType>
                <LineDescription>Utilities;Justification: W-IN-18-6402-1237845 </LineDescription>
                <LineGrossAmount>
                    <Value>113035.71</Value>
                    <Currency>
                        <Code>THB</Code>
                        <NumericCode>764</NumericCode>
                    </Currency>
                </LineGrossAmount>
                <UnitPrice>0</UnitPrice>
                <Quantity/>
                <UnitOfMeasure>
                    <Code/>
                    <Meaning/>
                </UnitOfMeasure>
                <Tax>
                    <TaxCode/>
                    <TaxRate>0</TaxRate>
                    <TaxRateCode/>
                </Tax>
            </DocumentPayableLine>
        </DocumentPayable>
    </OutboundPayment>
    <OutboundPayment>
        <DocumentPayable>
            <DocumentPayableLine>
                <LineNumber>2</LineNumber>
                <PONumber/>
                <ConsumptionAdviceNumber/>
                <LineType>
                    <Code>TAX</Code>
                    <Meaning/>
                </LineType>
                <LineDescription>The Garbage Service Fee For 02/64</LineDescription>
                <LineGrossAmount>
                    <Value>6730.5</Value>
                    <Currency>
                        <Code>THB</Code>
                        <NumericCode>764</NumericCode>
                    </Currency>
                </LineGrossAmount>
                <UnitPrice>0</UnitPrice>
                <Quantity/>
                <UnitOfMeasure>
                    <Code/>
                    <Meaning/>
                </UnitOfMeasure>
                <Tax>
                    <TaxCode>TH VAT</TaxCode>
                    <TaxRate>7</TaxRate>
                    <TaxRateCode>TH AP STD SVC 7</TaxRateCode>
                </Tax>
            </DocumentPayableLine>
            <DocumentPayableLine>
                <LineNumber>3</LineNumber>
                <PONumber/>
                <ConsumptionAdviceNumber/>
                <LineType>
                    <Code>AWT</Code>
                    <Meaning/>
                </LineType>
                <LineDescription>The Garbage Service Fee For 02/64</LineDescription>
                <LineGrossAmount>
                    <Value>-2884.5</Value>
                    <Currency>
                        <Code>THB</Code>
                        <NumericCode>764</NumericCode>
                    </Currency>
                </LineGrossAmount>
                <UnitPrice>0</UnitPrice>
                <Quantity/>
                <UnitOfMeasure>
                    <Code/>
                    <Meaning/>
                </UnitOfMeasure>
                <Tax>
                    <TaxCode>TH WHT</TaxCode>
                    <TaxRate>3</TaxRate>
                    <TaxRateCode>TH WHT SVCS 03</TaxRateCode>
                </Tax>
            </DocumentPayableLine>
            <DocumentPayableLine>
                <LineNumber>1</LineNumber>
                <PONumber/>
                <ConsumptionAdviceNumber/>
                <LineType>
                    <Code>ITEM</Code>
                    <Meaning/>
                </LineType>
                <LineDescription>The Garbage Service Fee For 02/64</LineDescription>
                <LineGrossAmount>
                    <Value>96150</Value>
                    <Currency>
                        <Code>THB</Code>
                        <NumericCode>764</NumericCode>
                    </Currency>
                </LineGrossAmount>
                <UnitPrice>0</UnitPrice>
                <Quantity/>
                <UnitOfMeasure>
                    <Code/>
                    <Meaning/>
                </UnitOfMeasure>
                <Tax>
                    <TaxCode/>
                    <TaxRate>0</TaxRate>
                    <TaxRateCode/>
                </Tax>
            </DocumentPayableLine>
        </DocumentPayable>
        <DocumentPayable>
            <DocumentPayableLine>
                <LineNumber>2</LineNumber>
                <PONumber/>
                <ConsumptionAdviceNumber/>
                <LineType>
                    <Code>TAX</Code>
                    <Meaning/>
                </LineType>
                <LineDescription> Monthly billing on Feb'21 of supplier 304 INDUSTRIAL PARK.( Waste Water )</LineDescription>
                <LineGrossAmount>
                    <Value>28276.42</Value>
                    <Currency>
                        <Code>THB</Code>
                        <NumericCode>764</NumericCode>
                    </Currency>
                </LineGrossAmount>
                <UnitPrice>0</UnitPrice>
                <Quantity/>
                <UnitOfMeasure>
                    <Code/>
                    <Meaning/>
                </UnitOfMeasure>
                <Tax>
                    <TaxCode>TH VAT</TaxCode>
                    <TaxRate>7</TaxRate>
                    <TaxRateCode>TH AP STD SVC 7</TaxRateCode>
                </Tax>
            </DocumentPayableLine>
            <DocumentPayableLine>
                <LineNumber>3</LineNumber>
                <PONumber/>
                <ConsumptionAdviceNumber/>
                <LineType>
                    <Code>AWT</Code>
                    <Meaning/>
                </LineType>
                <LineDescription> Monthly billing on Feb'21 of supplier 304 INDUSTRIAL PARK.( Waste Water )</LineDescription>
                <LineGrossAmount>
                    <Value>-12118.46</Value>
                    <Currency>
                        <Code>THB</Code>
                        <NumericCode>764</NumericCode>
                    </Currency>
                </LineGrossAmount>
                <UnitPrice>0</UnitPrice>
                <Quantity/>
                <UnitOfMeasure>
                    <Code/>
                    <Meaning/>
                </UnitOfMeasure>
                <Tax>
                    <TaxCode>TH WHT</TaxCode>
                    <TaxRate>2.999999</TaxRate>
                    <TaxRateCode>TH WHT SVCS 03</TaxRateCode>
                </Tax>
            </DocumentPayableLine>
            <DocumentPayableLine>
                <LineNumber>1</LineNumber>
                <PONumber/>
                <ConsumptionAdviceNumber/>
                <LineType>
                    <Code>ITEM</Code>
                    <Meaning/>
                </LineType>
                <LineDescription> Monthly billing on Feb'21 of supplier 304 INDUSTRIAL PARK.( Waste Water )</LineDescription>
                <LineGrossAmount>
                    <Value>403948.8</Value>
                    <Currency>
                        <Code>THB</Code>
                        <NumericCode>764</NumericCode>
                    </Currency>
                </LineGrossAmount>
                <UnitPrice>0</UnitPrice>
                <Quantity/>
                <UnitOfMeasure>
                    <Code/>
                    <Meaning/>
                </UnitOfMeasure>
                <Tax>
                    <TaxCode/>
                    <TaxRate>0</TaxRate>
                    <TaxRateCode/>
                </Tax>
            </DocumentPayableLine>
        </DocumentPayable>
        <DocumentPayable>
            <DocumentPayableLine>
                <LineNumber>2</LineNumber>
                <PONumber/>
                <ConsumptionAdviceNumber/>
                <LineType>
                    <Code>TAX</Code>
                    <Meaning/>
                </LineType>
                <LineDescription>Monthly billing on Feb'21 of supplier 304 INDUSTRIAL PARK.( Waste Water )</LineDescription>
                <LineGrossAmount>
                    <Value>13296.15</Value>
                    <Currency>
                        <Code>THB</Code>
                        <NumericCode>764</NumericCode>
                    </Currency>
                </LineGrossAmount>
                <UnitPrice>0</UnitPrice>
                <Quantity/>
                <UnitOfMeasure>
                    <Code/>
                    <Meaning/>
                </UnitOfMeasure>
                <Tax>
                    <TaxCode>TH VAT</TaxCode>
                    <TaxRate>7</TaxRate>
                    <TaxRateCode>TH AP STD SVC 7</TaxRateCode>
                </Tax>
            </DocumentPayableLine>
            <DocumentPayableLine>
                <LineNumber>3</LineNumber>
                <PONumber/>
                <ConsumptionAdviceNumber/>
                <LineType>
                    <Code>AWT</Code>
                    <Meaning/>
                </LineType>
                <LineDescription>Monthly billing on Feb'21 of supplier 304 INDUSTRIAL PARK.( Waste Water )</LineDescription>
                <LineGrossAmount>
                    <Value>-5698.35</Value>
                    <Currency>
                        <Code>THB</Code>
                        <NumericCode>764</NumericCode>
                    </Currency>
                </LineGrossAmount>
                <UnitPrice>0</UnitPrice>
                <Quantity/>
                <UnitOfMeasure>
                    <Code/>
                    <Meaning/>
                </UnitOfMeasure>
                <Tax>
                    <TaxCode>TH WHT</TaxCode>
                    <TaxRate>3</TaxRate>
                    <TaxRateCode>TH WHT SVCS 03</TaxRateCode>
                </Tax>
            </DocumentPayableLine>
            <DocumentPayableLine>
                <LineNumber>1</LineNumber>
                <PONumber/>
                <ConsumptionAdviceNumber/>
                <LineType>
                    <Code>ITEM</Code>
                    <Meaning/>
                </LineType>
                <LineDescription>Monthly billing on Feb'21 of supplier 304 INDUSTRIAL PARK.( Waste Water )</LineDescription>
                <LineGrossAmount>
                    <Value>189945</Value>
                    <Currency>
                        <Code>THB</Code>
                        <NumericCode>764</NumericCode>
                    </Currency>
                </LineGrossAmount>
                <UnitPrice>0</UnitPrice>
                <Quantity/>
                <UnitOfMeasure>
                    <Code/>
                    <Meaning/>
                </UnitOfMeasure>
                <Tax>
                    <TaxCode/>
                    <TaxRate>0</TaxRate>
                    <TaxRateCode/>
                </Tax>
            </DocumentPayableLine>
        </DocumentPayable>
        <DocumentPayable>
            <DocumentPayableLine>
                <LineNumber>2</LineNumber>
                <PONumber/>
                <ConsumptionAdviceNumber/>
                <LineType>
                    <Code>TAX</Code>
                    <Meaning/>
                </LineType>
                <LineDescription>Monthly billing on Feb'21 of supplier 304 INDUSTRIAL PARK.( Rental Car parking ) Cloud GL : 630086 Cloud CC : 50020 Invoice = VAT 0%</LineDescription>
                <LineGrossAmount>
                    <Value>0</Value>
                    <Currency>
                        <Code>THB</Code>
                        <NumericCode>764</NumericCode>
                    </Currency>
                </LineGrossAmount>
                <UnitPrice>0</UnitPrice>
                <Quantity/>
                <UnitOfMeasure>
                    <Code/>
                    <Meaning/>
                </UnitOfMeasure>
                <Tax>
                    <TaxCode>TH VAT</TaxCode>
                    <TaxRate>0</TaxRate>
                    <TaxRateCode>TH AP ZERO 0</TaxRateCode>
                </Tax>
            </DocumentPayableLine>
            <DocumentPayableLine>
                <LineNumber>3</LineNumber>
                <PONumber/>
                <ConsumptionAdviceNumber/>
                <LineType>
                    <Code>AWT</Code>
                    <Meaning/>
                </LineType>
                <LineDescription>Monthly billing on Feb'21 of supplier 304 INDUSTRIAL PARK.( Rental Car parking ) Cloud GL : 630086 Cloud CC : 50020 Invoice = VAT 0%</LineDescription>
                <LineGrossAmount>
                    <Value>-13763.2</Value>
                    <Currency>
                        <Code>THB</Code>
                        <NumericCode>764</NumericCode>
                    </Currency>
                </LineGrossAmount>
                <UnitPrice>0</UnitPrice>
                <Quantity/>
                <UnitOfMeasure>
                    <Code/>
                    <Meaning/>
                </UnitOfMeasure>
                <Tax>
                    <TaxCode>TH WHT</TaxCode>
                    <TaxRate>5</TaxRate>
                    <TaxRateCode>TH WHT RENT 05</TaxRateCode>
                </Tax>
            </DocumentPayableLine>
            <DocumentPayableLine>
                <LineNumber>1</LineNumber>
                <PONumber/>
                <ConsumptionAdviceNumber/>
                <LineType>
                    <Code>ITEM</Code>
                    <Meaning/>
                </LineType>
                <LineDescription>Monthly billing on Feb'21 of supplier 304 INDUSTRIAL PARK.( Rental Car parking )Cloud GL : 630086 Cloud CC : 50020 Invoice = VAT 0%</LineDescription>
                <LineGrossAmount>
                    <Value>275264</Value>
                    <Currency>
                        <Code>THB</Code>
                        <NumericCode>764</NumericCode>
                    </Currency>
                </LineGrossAmount>
                <UnitPrice>0</UnitPrice>
                <Quantity/>
                <UnitOfMeasure>
                    <Code/>
                    <Meaning/>
                </UnitOfMeasure>
                <Tax>
                    <TaxCode/>
                    <TaxRate>0</TaxRate>
                    <TaxRateCode/>
                </Tax>
            </DocumentPayableLine>
        </DocumentPayable>
    </OutboundPayment>
</OutboundPaymentInstruction>

请帮忙,因为我正在尝试为单个“AWT”场景执行以下逻辑,最后通过按 TaxRateCode 为其自己的 OutboundPayment 标签分组来总结总数。

Forumla 尝试实施 = (100 div (Tax/TaxRate)) * LineGrossAmount/Value

Example - ((100 div 3) * -5698.35) = -189945
          ((100 div 2.999999) * -12118.46) = -403948.8013
          ((100 div 3) * -2884.5) = -96150

 <calc>690043.80</calc>

预期输出

<Result>       
                <Rcrd>
                  <TaxAmt>
                     <Rate>3</Rate>
                     <calc>690043.80</calc>
                  </TaxAmt>
               </Rcrd>
               <Rcrd>
                  <TaxAmt>
                     <Rate>5</Rate>
                     <calc>275264.00</calc>
                  </TaxAmt>
               </Rcrd>
               
</Result>
<Result>
               <Rcrd>
                  <TaxAmt>
                     <Rate>3</Rate>
                     <calc>113035.7043</calc>
                  </TaxAmt>
               </Rcrd>
</Result>

代码尝试

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

<xsl:key name="k" match="DocumentPayableLine[LineType/Code='AWT']" use="concat(generate-id(../..), Tax/TaxRateCode)" />

<xsl:template match="/OutboundPaymentInstruction">
    <xsl:for-each select="OutboundPayment">
        <Result>
            <xsl:for-each select="DocumentPayable/DocumentPayableLine[LineType/Code='AWT'][count(. | key('k', concat(generate-id(../..), Tax/TaxRateCode))[1]) = 1]">
                <Rcrd>
                         <xsl:variable name="TX1"><xsl:value-of select="Tax/TaxRate"/></xsl:variable>
                         <xsl:variable name="GrossAmount"><xsl:value-of select="LineGrossAmount/Value"/></xsl:variable>         
                    <TaxAmt>
                        <Rate>
                            <xsl:value-of select="round(Tax/TaxRate)"/>
                        </Rate>
                        
                        <xsl:variable name="IndividualCalc"><xsl:value-of select="((100 div $TX1) * $GrossAmount)"/></xsl:variable>
                        <calc>      
                         <xsl:value-of select="-sum(key('k', concat(generate-id(../..), Tax/TaxRateCode))$IndividualCalc)"/>
                        </calc>
                    </TaxAmt>
                </Rcrd>
            </xsl:for-each>
        </Result>
    </xsl:for-each>
    
    </xsl:template>

</xsl:stylesheet>

请帮忙。

【问题讨论】:

  • 我听不懂你的解释。您的第一组的总 LineGrossAmount/Value 为 -3391.07,税率为 3,因此我预计税额为 101.7321。我在您的问题中没有看到这些数字。
  • @michael.hor257k - 嗨 Michael,“OutboundPayment”标签之一只有一个“AWT”场景,其 LineGrossAmount/Value 为 -3391.07。因此,我们尝试执行的公式是 fromat_number((100 / 2.999999) * -3391.07) = 113035.7043。请帮忙

标签: xslt grouping xslt-1.0 xslt-grouping muenchian-grouping


【解决方案1】:

我认为,如果没有 XPath 2 或更高版本的表达能力,一种方法是将各个值存储在 XML 结果树片段中,并将其转换为节点集以对值求和:

<xsl:template match="/OutboundPaymentInstruction">
    <xsl:for-each select="OutboundPayment">
        <Result>
            <xsl:for-each select="DocumentPayable/DocumentPayableLine[LineType/Code='AWT'][count(. | key('k', concat(generate-id(../..), Tax/TaxRateCode))[1]) = 1]">
                <xsl:variable name="amounts">
                  <xsl:for-each select="key('k', concat(generate-id(../..), Tax/TaxRateCode))">
                    <amount>
                      <xsl:value-of select="100 div Tax/TaxRate * LineGrossAmount/Value"/>
                    </amount>
                  </xsl:for-each>
                </xsl:variable>
                <Rcrd>
                    <TaxAmt>
                        <Rate>
                            <xsl:value-of select="round(Tax/TaxRate)"/>
                        </Rate>
                        
                        <calc>      
                         <xsl:value-of select="format-number(-sum(exsl:node-set($amounts)/amount), '0.00')"/>
                        </calc>
                    </TaxAmt>
                </Rcrd>
            </xsl:for-each>
        </Result>
    </xsl:for-each>
</xsl:template>

我得到了您显示的值,但顺序不同:

<Result>
  <Rcrd>
    <TaxAmt>
      <Rate>3</Rate>
      <calc>113035.70</calc>
    </TaxAmt>
  </Rcrd>
</Result>
<Result>
  <Rcrd>
    <TaxAmt>
      <Rate>3</Rate>
      <calc>690043.80</calc>
    </TaxAmt>
  </Rcrd>
  <Rcrd>
    <TaxAmt>
      <Rate>5</Rate>
      <calc>275264.00</calc>
    </TaxAmt>
  </Rcrd>
</Result>

XSLT 1 解决方案需要您在 XSLT 样式表中声明 xmlns:exsl="http://exslt.org/common"(或者,对于 MSXML,将其更改为专有的 xmlns:msxml="urn:schemas-microsoft-com:xslt" 并使用 msxml:node-set 而不是 exsl:node-set)。

【讨论】:

  • 嗨@Martin Honnen,使用上面给定的代码后,我在 标记中得到了 -0.00 的结果。请找到我现有的 XSL 1.0 版本中也包含的代码行。请帮助``` w3.org/2001/XMLSchema-instance" > ```
  • 您在原始问题中的哪个位置使用Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03"?请不要在发布问题后的几天内更改要求。对于未使用命名空间的问题中的代码,建议的答案会生成我在答案中显示的代码,其中包含两个 Result 元素,其中第一个元素有一个 Rcrd&lt;calc&gt;113035.70&lt;/calc&gt;,第二个元素有两个他们,第一个是&lt;calc&gt;690043.80&lt;/calc&gt;,第二个是&lt;calc&gt;275264.00&lt;/calc&gt;
  • 如果您尝试使用结果命名空间,请不要将其用于临时元素,例如通过使用&lt;amount xmlns=""&gt;..&lt;/amount&gt;。否则 XPath 选择不能工作exsl:node-set($amounts)/amount.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-22
  • 1970-01-01
  • 2018-03-25
  • 1970-01-01
  • 2016-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多