如何在下面的 XSLT 1.0 代码中进行分组。需要按 TaxRateCode 分组

Posted

技术标签:

【中文标题】如何在下面的 XSLT 1.0 代码中进行分组。需要按 TaxRateCode 分组【英文标题】:How to group by in XSLT 1.0 code below. Need to group by TaxRateCode 【发布时间】:2021-12-23 01:17:33 【问题描述】:

我需要根据“TaxRateCode”标签进行分组并根据“LineType/Code='AWT'”进行过滤 并对每个 OutboundPayment 标签的“LineGrossAmount/Value”进行求和。

下面的XML

<?xml version="1.0" encoding="UTF-8"?>
<OutboundPaymentInstruction>
   <OutboundPayment>
      <DocumentPayable>
         <DocumentPayableLine>
            <LineType>
               <Code>TAX</Code>
               <Meaning />
            </LineType>
            <LineDescription>CHANDLER BING</LineDescription>
            <LineGrossAmount>
               <Value>3098.76</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode>TH VAT</TaxCode>
               <TaxRate>7</TaxRate>
               <TaxRateCode>TH AP STD SVC 7</TaxRateCode>
            </Tax>
         </DocumentPayableLine>
         <DocumentPayableLine>
            <LineType>
               <Code>AWT</Code>
               <Meaning />
            </LineType>
            <LineDescription>CHANDLER BING</LineDescription>
            <LineGrossAmount>
               <Value>-1328.04</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode>TH WHT</TaxCode>
               <TaxRate>3</TaxRate>
               <TaxRateCode>TH WHT SVCS 03</TaxRateCode>
            </Tax>
         </DocumentPayableLine>
         <DocumentPayableLine>
            <LineType>
               <Code>ITEM</Code>
               <Meaning />
            </LineType>
            <LineDescription>CHANDLER - INV2103-0185</LineDescription>
            <LineGrossAmount>
               <Value>44268</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode />
               <TaxRate>0</TaxRate>
               <TaxRateCode />
            </Tax>
         </DocumentPayableLine>
      </DocumentPayable>
   </OutboundPayment>
   <OutboundPayment>
      <DocumentPayable>
         <DocumentPayableLine>
            <LineType>
               <Code>TAX</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT-760589905 checkin</LineDescription>
            <LineGrossAmount>
               <Value>2241.33</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode>TH VAT</TaxCode>
               <TaxRate>7</TaxRate>
               <TaxRateCode>TH AP STD SVC 7</TaxRateCode>
            </Tax>
         </DocumentPayableLine>
         <DocumentPayableLine>
            <LineType>
               <Code>AWT</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT-760589905 checkin</LineDescription>
            <LineGrossAmount>
               <Value>-1525</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode>TH WHT</TaxCode>
               <TaxRate>5</TaxRate>
               <TaxRateCode>TH WHT RENT 05</TaxRateCode>
            </Tax>
         </DocumentPayableLine>
         <DocumentPayableLine>
            <LineType>
               <Code>AWT</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT-760589905 (21/12/2563 - 20/01/2564 ) Voice for outbound and inbound (Local Call) Link 2</LineDescription>
            <LineGrossAmount>
               <Value>-45.57</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode>TH WHT</TaxCode>
               <TaxRate>3</TaxRate>
               <TaxRateCode>TH WHT SVCS 03</TaxRateCode>
            </Tax>
         </DocumentPayableLine>
         <DocumentPayableLine>
            <LineType>
               <Code>ITEM</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT-760589905 (21/12/2563 - 20/01/2564 ) Voice for outbound and inbound (Local Call) Link 2</LineDescription>
            <LineGrossAmount>
               <Value>30500</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode />
               <TaxRate>0</TaxRate>
               <TaxRateCode />
            </Tax>
         </DocumentPayableLine>
         <DocumentPayableLine>
            <LineType>
               <Code>ITEM</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT-760589905 (21/12/2563 - 20/01/2564 ) Voice for outbound and inbound (Local Call) Link 2</LineDescription>
            <LineGrossAmount>
               <Value>1519</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode />
               <TaxRate>0</TaxRate>
               <TaxRateCode />
            </Tax>
         </DocumentPayableLine>
      </DocumentPayable>
      <DocumentPayable>
         <DocumentPayableLine>
            <LineType>
               <Code>TAX</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT-763687229. ( 21/01/2564 - 20/02/2564 ) Voice for outbound and inbound (Local Call) Link 2</LineDescription>
            <LineGrossAmount>
               <Value>2241.4</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode>TH VAT</TaxCode>
               <TaxRate>7</TaxRate>
               <TaxRateCode>TH AP STD SVC 7</TaxRateCode>
            </Tax>
         </DocumentPayableLine>
         <DocumentPayableLine>
            <LineType>
               <Code>AWT</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT-763687229. ( 21/01/2564 - 20/02/2564 ) Voice for outbound and inbound (Local Call) Link 2</LineDescription>
            <LineGrossAmount>
               <Value>-1525</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode>TH WHT</TaxCode>
               <TaxRate>5</TaxRate>
               <TaxRateCode>TH WHT RENT 05</TaxRateCode>
            </Tax>
         </DocumentPayableLine>
         <DocumentPayableLine>
            <LineType>
               <Code>AWT</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT-763687229. ( 21/01/2564 - 20/02/2564 ) Voice for outbound and inbound (Local Call) Link 2</LineDescription>
            <LineGrossAmount>
               <Value>-45.6</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode>TH WHT</TaxCode>
               <TaxRate>3</TaxRate>
               <TaxRateCode>TH WHT SVCS 03</TaxRateCode>
            </Tax>
         </DocumentPayableLine>
         <DocumentPayableLine>
            <LineType>
               <Code>ITEM</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT-763687229. ( 21/01/2564 - 20/02/2564 )Voice for outbound and inbound (Local Call) Link 2</LineDescription>
            <LineGrossAmount>
               <Value>30500</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode />
               <TaxRate>0</TaxRate>
               <TaxRateCode />
            </Tax>
         </DocumentPayableLine>
         <DocumentPayableLine>
            <LineType>
               <Code>ITEM</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT-763687229. ( 21/01/2564 - 20/02/2564 )Voice for outbound and inbound (Local Call) Link 2</LineDescription>
            <LineGrossAmount>
               <Value>1520</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode />
               <TaxRate>0</TaxRate>
               <TaxRateCode />
            </Tax>
         </DocumentPayableLine>
      </DocumentPayable>
      <DocumentPayable>
         <DocumentPayableLine>
            <LineType>
               <Code>TAX</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT- 763687230 Utilities</LineDescription>
            <LineGrossAmount>
               <Value>3193.33</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode>TH VAT</TaxCode>
               <TaxRate>7</TaxRate>
               <TaxRateCode>TH AP STD SVC 7</TaxRateCode>
            </Tax>
         </DocumentPayableLine>
         <DocumentPayableLine>
            <LineType>
               <Code>AWT</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT- 763687230 Utilities</LineDescription>
            <LineGrossAmount>
               <Value>-2110</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode>TH WHT</TaxCode>
               <TaxRate>5</TaxRate>
               <TaxRateCode>TH WHT RENT 05</TaxRateCode>
            </Tax>
         </DocumentPayableLine>
         <DocumentPayableLine>
            <LineType>
               <Code>AWT</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT- 763687230 Utilities</LineDescription>
            <LineGrossAmount>
               <Value>-102.57</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode>TH WHT</TaxCode>
               <TaxRate>3</TaxRate>
               <TaxRateCode>TH WHT SVCS 03</TaxRateCode>
            </Tax>
         </DocumentPayableLine>
         <DocumentPayableLine>
            <LineType>
               <Code>ITEM</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT- 763687230 Utilities</LineDescription>
            <LineGrossAmount>
               <Value>42200</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode />
               <TaxRate>0</TaxRate>
               <TaxRateCode />
            </Tax>
         </DocumentPayableLine>
         <DocumentPayableLine>
            <LineType>
               <Code>ITEM</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT- 763687230 Utilities</LineDescription>
            <LineGrossAmount>
               <Value>3419</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode />
               <TaxRate>0</TaxRate>
               <TaxRateCode />
            </Tax>
         </DocumentPayableLine>
      </DocumentPayable>
      <DocumentPayable>
         <DocumentPayableLine>
            <LineType>
               <Code>TAX</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT 763955166 Utilities</LineDescription>
            <LineGrossAmount>
               <Value>1.81</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode>TH VAT</TaxCode>
               <TaxRate>7</TaxRate>
               <TaxRateCode>TH AP STD SVC 7</TaxRateCode>
            </Tax>
         </DocumentPayableLine>
         <DocumentPayableLine>
            <LineType>
               <Code>AWT</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT 763955166 Utilities</LineDescription>
            <LineGrossAmount>
               <Value>-1.29</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode>TH WHT</TaxCode>
               <TaxRate>4.998063</TaxRate>
               <TaxRateCode>TH WHT RENT 05</TaxRateCode>
            </Tax>
         </DocumentPayableLine>
         <DocumentPayableLine>
            <LineType>
               <Code>ITEM</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT 763955166 Utilities</LineDescription>
            <LineGrossAmount>
               <Value>25.81</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode />
               <TaxRate>0</TaxRate>
               <TaxRateCode />
            </Tax>
         </DocumentPayableLine>
      </DocumentPayable>
      <DocumentPayable>
         <DocumentPayableLine>
            <LineType>
               <Code>TAX</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT 763955167 Utilities</LineDescription>
            <LineGrossAmount>
               <Value>1.81</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode>TH VAT</TaxCode>
               <TaxRate>7</TaxRate>
               <TaxRateCode>TH AP STD SVC 7</TaxRateCode>
            </Tax>
         </DocumentPayableLine>
         <DocumentPayableLine>
            <LineType>
               <Code>AWT</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT 763955167 Utilities</LineDescription>
            <LineGrossAmount>
               <Value>-1.29</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode>TH WHT</TaxCode>
               <TaxRate>4.998063</TaxRate>
               <TaxRateCode>TH WHT RENT 05</TaxRateCode>
            </Tax>
         </DocumentPayableLine>
         <DocumentPayableLine>
            <LineType>
               <Code>ITEM</Code>
               <Meaning />
            </LineType>
            <LineDescription>TOT 763955167 Utilities</LineDescription>
            <LineGrossAmount>
               <Value>25.81</Value>
               <Currency>
                  <Code>THB</Code>
                  <NumericCode>764</NumericCode>
               </Currency>
            </LineGrossAmount>
            <Tax>
               <TaxCode />
               <TaxRate>0</TaxRate>
               <TaxRateCode />
            </Tax>
         </DocumentPayableLine>
      </DocumentPayable>
   </OutboundPayment>
</OutboundPaymentInstruction>

预期输出

<TAX>
               <Rcrd>
                  <TaxAmt>
                     <Rate>3</Rate>
                     <calc>1328.04</calc>
                  </TaxAmt>
               </Rcrd>
</TAX>
<TAX>          
               <Rcrd>
                  <TaxAmt>
                     <Rate>5</Rate>
                     <calc>5162.58</calc>
                  </TaxAmt>
               </Rcrd>
               <Rcrd>
                  <TaxAmt>
                     <Rate>3</Rate>
                     <calc>193.74</calc>
                  </TaxAmt>
               </Rcrd>
</TAX>

我已尝试使用以下代码,但它没有给出预期的输出。在其自己的“OutboundPayment”标签中使用“DocumentPayable”标签时出现问题。请帮助解决问题。另请注意,我们的要求仅支持 XSLT 1.0 版本,因此我使用了 Muenchain 分组方法。

代码尝试

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes"/>
    <xsl:key name="group1" match="DocumentPayable/DocumentPayableLine" use="concat(generate-id(..), Tax/TaxRateCode)"/>
    <xsl:template match="/">
        <xsl:for-each select="/OutboundPaymentInstruction/OutboundPayment">
            <TAX>
                <xsl:for-each select="DocumentPayable/DocumentPayableLine[count(. | key('group1', concat(generate-id(..), Tax/TaxRateCode))[1]) = 1]">
                    <xsl:if test="(LineType/Code='AWT')">
                        <xsl:variable name="LocationCode" select="Tax/TaxRateCode"/>
                        <Rcrd>
                            <TaxAmt>
                                <Rate>
                                    <xsl:if test="not(Tax/TaxRate='')">
                                        <xsl:value-of select="round(Tax/TaxRate)"/>
                                    </xsl:if>
                                </Rate>
                                <xsl:variable name="calcualtedVal1">
                                    <xsl:value-of select="translate(sum(//DocumentPayableLine[./Tax/TaxRateCode= $LocationCode]/LineGrossAmount/Value),'-','')"/>
                                </xsl:variable>
                                <calc>
                                    <xsl:value-of select="$calcualtedVal1"/>
                                </calc>
                            </TaxAmt>
                        </Rcrd>
                    </xsl:if>
                </xsl:for-each>
            </TAX>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

【问题讨论】:

【参考方案1】:

AFAICT,可能很简单:

XSLT 1.0

<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">
        <TAX>
            <xsl:for-each select="DocumentPayable/DocumentPayableLine[LineType/Code='AWT'][count(. | key('k', concat(generate-id(../..), Tax/TaxRateCode))[1]) = 1]">
                <Rcrd>
                    <TaxAmt>
                        <Rate>
                            <xsl:value-of select="Tax/TaxRate"/>
                        </Rate>
                        <calc>
                            <xsl:value-of select="-sum(key('k', concat(generate-id(../..), Tax/TaxRateCode))/LineGrossAmount/Value)"/>
                        </calc>
                    </TaxAmt>
                </Rcrd>
            </xsl:for-each>
        </TAX>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

请注意,结果是 XML 片段,而不是格式良好的 XML 文档,因为它没有单个根元素。

【讨论】:

以上是关于如何在下面的 XSLT 1.0 代码中进行分组。需要按 TaxRateCode 分组的主要内容,如果未能解决你的问题,请参考以下文章

使用 XSLT 1.0 对值进行分组

XSLT 1.0 对节点进行分组和计数

Muenchian 分组 XSLT 1.0 多重分组

xslt 1.0中的嵌套分组

是否可以使用 XSLT 1.0 对条目进行分组?

XSLT 1.0 按子节点的值对 xml 节点进行分组