XSLT 代码 - 为每 3 xml 段创建一个新的 XML 段

XSLT code - Create a new XML segment for every 3 xml segments

我仍在学习和提高我的 XSLT 技能。但希望我能从专家那里看到我的要求没关系。

对于每 3 条记录 xml 段,将在顶部创建一个新记录 xml 段,该段将计算 3 条记录的总和。然后,新记录 (PK50) 段仍将产生多余的或剩余的行项目。请参阅下面的示例数据。已创建 PK50 条记录,其中最后一条包含剩余 2 条记录的总和。

PostingKey 50表示新创建的记录,每3条记录的金额总和。 Account 字段也将具有值“Summation”。感谢您的任何意见。非常感谢!

源数据:

<?xml version="1.0" encoding="UTF-8"?>
<ns1:AccrualReport xmlns:ns1="http://client.com/expense/expensereport">
    <Record>
        <CoCode>BI10</CoCode>
        <DocDate>25/01/2022</DocDate>
        <PostDate>25/01/2022</PostDate>
        <DocType>SB</DocType>
        <PostingKey>40</PostingKey>
        <Account>lines</Account>
        <Amt_DocCurr>12.5</Amt_DocCurr>
        <Amt_LocCurr>12.5</Amt_LocCurr>
    </Record>
    <Record>
        <CoCode>BI10</CoCode>
        <DocDate>25/01/2022</DocDate>
        <PostDate>25/01/2022</PostDate>
        <DocType>SB</DocType>
        <PostingKey>40</PostingKey>
        <Account>lines</Account>
        <Amt_DocCurr>22</Amt_DocCurr>
        <Amt_LocCurr>22</Amt_LocCurr>
    </Record>
    <Record>
        <CoCode>BI10</CoCode>
        <DocDate>25/01/2022</DocDate>
        <PostDate>25/01/2022</PostDate>
        <DocType>SB</DocType>
        <PostingKey>40</PostingKey>
        <Account>60875000</Account>
        <Amt_DocCurr>1.15</Amt_DocCurr>
        <Amt_LocCurr>1.15</Amt_LocCurr>
    </Record>
    <Record>
        <CoCode>BI10</CoCode>
        <DocDate>25/01/2022</DocDate>
        <PostDate>25/01/2022</PostDate>
        <DocType>SB</DocType>
        <PostingKey>40</PostingKey>
        <Account>60875000</Account>
        <Amt_DocCurr>20</Amt_DocCurr>
        <Amt_LocCurr>20</Amt_LocCurr>
    </Record>
    <Record>
        <CoCode>BI10</CoCode>
        <DocDate>25/01/2022</DocDate>
        <PostDate>25/01/2022</PostDate>
        <DocType>SB</DocType>
        <PostingKey>40</PostingKey>
        <Account>60875000</Account>
        <Amt_DocCurr>1</Amt_DocCurr>
        <Amt_LocCurr>1</Amt_LocCurr>
    </Record>
    <Record>
        <CoCode>BI10</CoCode>
        <DocDate>25/01/2022</DocDate>
        <PostDate>25/01/2022</PostDate>
        <DocType>SB</DocType>
        <PostingKey>40</PostingKey>
        <Account>60875000</Account>
        <Amt_DocCurr>2</Amt_DocCurr>
        <Amt_LocCurr>2</Amt_LocCurr>
    </Record>
    <Record>
        <CoCode>BI10</CoCode>
        <DocDate>25/01/2022</DocDate>
        <PostDate>25/01/2022</PostDate>
        <DocType>SB</DocType>
        <PostingKey>40</PostingKey>
        <Account>60875000</Account>
        <Amt_DocCurr>1</Amt_DocCurr>
        <Amt_LocCurr>1</Amt_LocCurr>
    </Record>
    <Record>
        <CoCode>BI10</CoCode>
        <DocDate>25/01/2022</DocDate>
        <PostDate>25/01/2022</PostDate>
        <DocType>SB</DocType>
        <PostingKey>40</PostingKey>
        <Account>60875000</Account>
        <Amt_DocCurr>1</Amt_DocCurr>
        <Amt_LocCurr>1</Amt_LocCurr>
    </Record>
</ns1:AccrualReport>

输出预期数据:

<?xml version="1.0" encoding="UTF-8"?>
<ns1:AccrualReport xmlns:ns1="http://client.com/expense/expensereport">
    <Record>
        <CoCode>BI10</CoCode>
        <DocDate>25/01/2022</DocDate>
        <PostDate>25/01/2022</PostDate>
        <DocType>SB</DocType>
        <PostingKey>50</PostingKey>
        <Account>Summation</Account>
        <Amt_DocCurr>35.65</Amt_DocCurr>
        <Amt_LocCurr>35.65</Amt_LocCurr>
    </Record>
    <Record>
        <CoCode>BI10</CoCode>
        <DocDate>25/01/2022</DocDate>
        <PostDate>25/01/2022</PostDate>
        <DocType>SB</DocType>
        <PostingKey>40</PostingKey>
        <Account>lines</Account>
        <Amt_DocCurr>12.5</Amt_DocCurr>
        <Amt_LocCurr>12.5</Amt_LocCurr>
    </Record>
    <Record>
        <CoCode>BI10</CoCode>
        <DocDate>25/01/2022</DocDate>
        <PostDate>25/01/2022</PostDate>
        <DocType>SB</DocType>
        <PostingKey>40</PostingKey>
        <Account>lines</Account>
        <Amt_DocCurr>22</Amt_DocCurr>
        <Amt_LocCurr>22</Amt_LocCurr>
    </Record>
    <Record>
        <CoCode>BI10</CoCode>
        <DocDate>25/01/2022</DocDate>
        <PostDate>25/01/2022</PostDate>
        <DocType>SB</DocType>
        <PostingKey>40</PostingKey>
        <Account>60875000</Account>
        <Amt_DocCurr>1.15</Amt_DocCurr>
        <Amt_LocCurr>1.15</Amt_LocCurr>
    </Record>
    <Record>
        <CoCode>BI10</CoCode>
        <DocDate>25/01/2022</DocDate>
        <PostDate>25/01/2022</PostDate>
        <DocType>SB</DocType>
        <PostingKey>50</PostingKey>
        <Account>Summation</Account>
        <Amt_DocCurr>23</Amt_DocCurr>
        <Amt_LocCurr>23</Amt_LocCurr>
    </Record>
    <Record>
        <CoCode>BI10</CoCode>
        <DocDate>25/01/2022</DocDate>
        <PostDate>25/01/2022</PostDate>
        <DocType>SB</DocType>
        <PostingKey>40</PostingKey>
        <Account>60875000</Account>
        <Amt_DocCurr>20</Amt_DocCurr>
        <Amt_LocCurr>20</Amt_LocCurr>
    </Record>
    <Record>
        <CoCode>BI10</CoCode>
        <DocDate>25/01/2022</DocDate>
        <PostDate>25/01/2022</PostDate>
        <DocType>SB</DocType>
        <PostingKey>40</PostingKey>
        <Account>60875000</Account>
        <Amt_DocCurr>1</Amt_DocCurr>
        <Amt_LocCurr>1</Amt_LocCurr>
    </Record>
    <Record>
        <CoCode>BI10</CoCode>
        <DocDate>25/01/2022</DocDate>
        <PostDate>25/01/2022</PostDate>
        <DocType>SB</DocType>
        <PostingKey>40</PostingKey>
        <Account>60875000</Account>
        <Amt_DocCurr>2</Amt_DocCurr>
        <Amt_LocCurr>2</Amt_LocCurr>
    </Record>
    <Record>
        <CoCode>BI10</CoCode>
        <DocDate>25/01/2022</DocDate>
        <PostDate>25/01/2022</PostDate>
        <DocType>SB</DocType>
        <PostingKey>50</PostingKey>
        <Account>Summation</Account>
        <Amt_DocCurr>2</Amt_DocCurr>
        <Amt_LocCurr>2</Amt_LocCurr>
    </Record>
    <Record>
        <CoCode>BI10</CoCode>
        <DocDate>25/01/2022</DocDate>
        <PostDate>25/01/2022</PostDate>
        <DocType>SB</DocType>
        <PostingKey>40</PostingKey>
        <Account>60875000</Account>
        <Amt_DocCurr>1</Amt_DocCurr>
        <Amt_LocCurr>1</Amt_LocCurr>
    </Record>
    <Record>
        <CoCode>BI10</CoCode>
        <DocDate>25/01/2022</DocDate>
        <PostDate>25/01/2022</PostDate>
        <DocType>SB</DocType>
        <PostingKey>40</PostingKey>
        <Account>60875000</Account>
        <Amt_DocCurr>1</Amt_DocCurr>
        <Amt_LocCurr>1</Amt_LocCurr>
    </Record>
</ns1:AccrualReport>

到目前为止,这是我正在使用的当前代码。唯一的问题是 Amt_DocCurr 和 Amt_LocCurr 中金额的总和。感谢任何输入来处理这个问题并将其合并到我当前的代码中。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>
   <xsl:param name="rows" select="3"/>
    <xsl:template match="/*">
        <xsl:apply-templates select="Record[position() mod $rows = 1]"/>
    </xsl:template>
    <xsl:template match="Record">
        <Record>
            <CoCode><xsl:value-of select="CoCode"/></CoCode>
            <DocDate><xsl:value-of select="DocDate"/></DocDate>
            <PostDate><xsl:value-of select="PostDate"/></PostDate>
            <DocType><xsl:value-of select="DocType"/></DocType>
            <PostingKey>50</PostingKey>
            <Account>Summation</Account>
            <Amt_DocCurr><xsl:value-of select="Amt_DocCurr"/></Amt_DocCurr>
            <Amt_LocCurr><xsl:value-of select="Amt_LocCurr"/></Amt_LocCurr> 
        </Record>
        <xsl:copy-of select=".|following-sibling::Record[not(position() > $rows -1)]"/>
    </xsl:template>

    <xsl:template match="/">
        <ns1:AccrualReport xmlns:ns1="http://client.com/expense/expensereport">
            <xsl:apply-templates />
        </ns1:AccrualReport>
    </xsl:template>

</xsl:stylesheet>

这里有一个方法可以做到这一点:

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

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <AccrualReportOutput>
      <xsl:apply-templates select="AccrualReport/Record[position() mod 2 = 0]"/>
    </AccrualReportOutput>
  </xsl:template>
  
  <xsl:template match="Record">
    <xsl:variable name="group" select=". | preceding-sibling::Record[1]"/>
    <!-- Create new Summation Record -->
    <xsl:copy>
      <xsl:copy-of select="* except (Account, PostingKey, Amt_DocCurr, Amt_LocCurr)"/>
      <Account>Summation</Account>
      <PostingKey>50</PostingKey>
      <Amt_DocCurr><xsl:value-of select="sum($group/Amt_DocCurr)"/></Amt_DocCurr>
      <Amt_LocCurr><xsl:value-of select="sum($group/Amt_LocCurr)"/></Amt_LocCurr>
    </xsl:copy>
    <!-- Copy 2 Records of group -->
    <xsl:copy-of select="$group"/>
  </xsl:template>
  
</xsl:stylesheet>

看到它在这里工作:https://xsltfiddle.liberty-development.net/aB9NKz/1

(我已经删除了你的 ns1: 命名空间前缀,不知道那个命名空间是什么,在你的 XML 中未声明。)