简体   繁体   中英

Grouping inside for-each based on child element values in xslt 2.0

I have a XML as below -

<Page14 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:statutorytype="us-stat-2020-annual-stmt-pc"
        xmlns:xbrli="http://www.xbrl.org/2001/instance"
        xmlns:iso4217="http://www.xe.com/iso4217.htm"
        xmlns:stat="urn:www-iasa-org/tempuri">
      <RegionRow>
      <Region>HI</Region>
      <EachUniqueCombo>
         <CurrentGroup>20000|16 Workers' Compensation|IBNR Loss Reserves|Pooled</CurrentGroup>
         <LedgerAccount>20000</LedgerAccount>
         <LOB>16 Workers' Compensation</LOB>
         <SpendCategory>IBNR Loss Reserves</SpendCategory>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>-31</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>20100|16 Workers' Compensation|IBNR ALAE D&amp;CC Reserves|Pooled</CurrentGroup>
         <LedgerAccount>20100</LedgerAccount>
         <LOB>16 Workers' Compensation</LOB>
         <SpendCategory>IBNR ALAE D&amp;CC Reserves</SpendCategory>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>-1</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>24000|16 Workers' Compensation||Pooled</CurrentGroup>
         <LedgerAccount>24000</LedgerAccount>
         <LOB>16 Workers' Compensation</LOB>
         <SpendCategory/>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>-45.13</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>40000|16 Workers' Compensation||Pooled</CurrentGroup>
         <LedgerAccount>40000</LedgerAccount>
         <LOB>16 Workers' Compensation</LOB>
         <SpendCategory/>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>-65</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>40010|16 Workers' Compensation||Pooled</CurrentGroup>
         <LedgerAccount>40010</LedgerAccount>
         <LOB>16 Workers' Compensation</LOB>
         <SpendCategory/>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>-5.34</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>50000|16 Workers' Compensation|Change in IBNR Loss Reserves|Pooled</CurrentGroup>
         <LedgerAccount>50000</LedgerAccount>
         <LOB>16 Workers' Compensation</LOB>
         <SpendCategory>Change in IBNR Loss Reserves</SpendCategory>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>-16</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>50100|16 Workers' Compensation|Change in IBNR ALAE D&amp;CC Reserves|Pooled</CurrentGroup>
         <LedgerAccount>50100</LedgerAccount>
         <LOB>16 Workers' Compensation</LOB>
         <SpendCategory>Change in IBNR ALAE D&amp;CC Reserves</SpendCategory>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>1</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>51000|16 Workers' Compensation|Change in Commission Reserves|Pooled</CurrentGroup>
         <LedgerAccount>51000</LedgerAccount>
         <LOB>16 Workers' Compensation</LOB>
         <SpendCategory>Change in Commission Reserves</SpendCategory>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>-1</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>51000|16 Workers' Compensation|Paid Commissions|Pooled</CurrentGroup>
         <LedgerAccount>51000</LedgerAccount>
         <LOB>16 Workers' Compensation</LOB>
         <SpendCategory>Paid Commissions</SpendCategory>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>-0.05</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>20000|17.1 Other Liability - Bodily Injury, Occurrence|IBNR Loss Reserves|Pooled</CurrentGroup>
         <LedgerAccount>20000</LedgerAccount>
         <LOB>17.1 Other Liability - Bodily Injury, Occurrence</LOB>
         <SpendCategory>IBNR Loss Reserves</SpendCategory>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>-28</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>20100|17.1 Other Liability - Bodily Injury, Occurrence|IBNR ALAE D&amp;CC Reserves|Pooled</CurrentGroup>
         <LedgerAccount>20100</LedgerAccount>
         <LOB>17.1 Other Liability - Bodily Injury, Occurrence</LOB>
         <SpendCategory>IBNR ALAE D&amp;CC Reserves</SpendCategory>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>135</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>24000|17.1 Other Liability - Bodily Injury, Occurrence||Pooled</CurrentGroup>
         <LedgerAccount>24000</LedgerAccount>
         <LOB>17.1 Other Liability - Bodily Injury, Occurrence</LOB>
         <SpendCategory/>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>19.17</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>40000|17.1 Other Liability - Bodily Injury, Occurrence||Pooled</CurrentGroup>
         <LedgerAccount>40000</LedgerAccount>
         <LOB>17.1 Other Liability - Bodily Injury, Occurrence</LOB>
         <SpendCategory/>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>22</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>40010|17.1 Other Liability - Bodily Injury, Occurrence||Pooled</CurrentGroup>
         <LedgerAccount>40010</LedgerAccount>
         <LOB>17.1 Other Liability - Bodily Injury, Occurrence</LOB>
         <SpendCategory/>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>-21.24</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>50000|17.1 Other Liability - Bodily Injury, Occurrence|Change in IBNR Loss Reserves|Pooled</CurrentGroup>
         <LedgerAccount>50000</LedgerAccount>
         <LOB>17.1 Other Liability - Bodily Injury, Occurrence</LOB>
         <SpendCategory>Change in IBNR Loss Reserves</SpendCategory>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>-261</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>50100|17.1 Other Liability - Bodily Injury, Occurrence|Change in IBNR ALAE D&amp;CC Reserves|Pooled</CurrentGroup>
         <LedgerAccount>50100</LedgerAccount>
         <LOB>17.1 Other Liability - Bodily Injury, Occurrence</LOB>
         <SpendCategory>Change in IBNR ALAE D&amp;CC Reserves</SpendCategory>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>-135</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>51000|17.1 Other Liability - Bodily Injury, Occurrence|Change in Commission Reserves|Pooled</CurrentGroup>
         <LedgerAccount>51000</LedgerAccount>
         <LOB>17.1 Other Liability - Bodily Injury, Occurrence</LOB>
         <SpendCategory>Change in Commission Reserves</SpendCategory>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>1</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>51000|17.1 Other Liability - Bodily Injury, Occurrence|Paid Commissions|Pooled</CurrentGroup>
         <LedgerAccount>51000</LedgerAccount>
         <LOB>17.1 Other Liability - Bodily Injury, Occurrence</LOB>
         <SpendCategory>Paid Commissions</SpendCategory>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>-3.3</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>20000|17.1 Other Liability - Physical Damage, Occurrence|IBNR Loss Reserves|Pooled</CurrentGroup>
         <LedgerAccount>20000</LedgerAccount>
         <LOB>17.1 Other Liability - Physical Damage, Occurrence</LOB>
         <SpendCategory>IBNR Loss Reserves</SpendCategory>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>1</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>50000|17.1 Other Liability - Physical Damage, Occurrence|Change in IBNR Loss Reserves|Pooled</CurrentGroup>
         <LedgerAccount>50000</LedgerAccount>
         <LOB>17.1 Other Liability - Physical Damage, Occurrence</LOB>
         <SpendCategory>Change in IBNR Loss Reserves</SpendCategory>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>-1</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>20000|24 Surety|IBNR Loss Reserves|Pooled</CurrentGroup>
         <LedgerAccount>20000</LedgerAccount>
         <LOB>24 Surety</LOB>
         <SpendCategory>IBNR Loss Reserves</SpendCategory>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>-458</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>20100|24 Surety|IBNR ALAE D&amp;CC Reserves|Pooled</CurrentGroup>
         <LedgerAccount>20100</LedgerAccount>
         <LOB>24 Surety</LOB>
         <SpendCategory>IBNR ALAE D&amp;CC Reserves</SpendCategory>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>-24</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>24000|24 Surety||Pooled</CurrentGroup>
         <LedgerAccount>24000</LedgerAccount>
         <LOB>24 Surety</LOB>
         <SpendCategory/>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>354.28</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>40010|24 Surety||Pooled</CurrentGroup>
         <LedgerAccount>40010</LedgerAccount>
         <LOB>24 Surety</LOB>
         <SpendCategory/>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>-819</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>50000|24 Surety|Change in IBNR Loss Reserves|Pooled</CurrentGroup>
         <LedgerAccount>50000</LedgerAccount>
         <LOB>24 Surety</LOB>
         <SpendCategory>Change in IBNR Loss Reserves</SpendCategory>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>-292</Sum>
      </EachUniqueCombo>
      <EachUniqueCombo>
         <CurrentGroup>50100|24 Surety|Change in IBNR ALAE D&amp;CC Reserves|Pooled</CurrentGroup>
         <LedgerAccount>50100</LedgerAccount>
         <LOB>24 Surety</LOB>
         <SpendCategory>Change in IBNR ALAE D&amp;CC Reserves</SpendCategory>
         <PooledWorktag>Pooled</PooledWorktag>
         <Sum>24</Sum>
      </EachUniqueCombo>
   </RegionRow>
   <RegionRow>
   <Region>AL</Region>
    ----
   </RegionRow>
   
</Page14>

For the above XML, I need to write an xslt that runs for each RegionRow element and within each region, I need to group first based on LOB element and then under that I need to do a sum of element 'Sum' based on certain ledger accounts.

I am trying to write the xslt as below -

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wd="urn:com.workday.report/CR_Page_14_EMC"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:statutorytype="us-stat-2020-annual-stmt-pc"
    xmlns:xbrli="http://www.xbrl.org/2001/instance" xmlns:iso4217="http://www.xe.com/iso4217.htm"
    xmlns:stat="urn:www-iasa-org/tempuri" exclude-result-prefixes=" wd xs" version="2.0">
    
    <xsl:output indent="yes" method="xml"/>
    
    <xsl:variable name="single_quote">
        <xsl:text>'</xsl:text>
    </xsl:variable>
    <xsl:variable name="ampersand">
        <xsl:text>&amp;</xsl:text>
    </xsl:variable>
    
    <xsl:template match="Page14">
        <OutputOfNextXSL>
            <xsl:for-each select="RegionRow">
                <RowData>
                    <Region>
                        <xsl:value-of select="Region"/>
                    </Region>
                    <xsl:for-each-group select="EachUniqueCombo" group-by="EachUniqueCombo[LOB = '24 Surety']">
                        <CellOneAllied>
                            <!-- ledger account xyz, -->
                            <xsl:value-of
                                select="-1 * sum(EachUniqueCombo[LedgerAccount = '40000' or LedgerAccount = '20000']/xs:decimal(Sum))"/>
                            
                        </CellOneAllied>
<CellTwoAllied>
</CellTwoAllied>
---
--
                    </xsl:for-each-group>
<xsl:for-each-group select= EachUniqueCombo group-by=EachUniqueCombo[LOB = '16 Workers' Compensation']">
  <!-- Sum here based on one or multiple ledger accounts-->
</xsl:for-each-group>

The group by can be based on 1 LOB or multiple and similarly the ledger account/spend category inside that grouping based on which I need to do a sum can also be single or multiple. I am not able to get any value based on above group-by logic and then also cannot reach the ledger account or spend category inside the group to be able to do a sum. Can someone please help achieve this?

So you just want to "group" the same "LedgerAccount"-Tag with the sum von "Sum"-Tag in each "RegionRow"-Tag

Following XSLT-Code will make this:

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="#all" version="2.0">
  <xsl:output method="xml" encoding="UTF-8"/>
  <xsl:template match="/">
<RegionRow>

  <xsl:for-each select="//RegionRow"> <!-- seach all RegionRow-Tags -->
     <xsl:for-each-group select="./EachUniqueCombo" group-by="LedgerAccount"> <!-- in each RegionRow-Tag group same LedgerAccounts -->
     <xsl:variable name="key" select="current-grouping-key()"/> <!-- save current key in variable "key" to get the right siblings -->
          <EachUniqueCombo>
               <key><xsl:value-of select="$key"/></key>
               <Sum><xsl:value-of select="sum(//Sum[../LedgerAccount/text()=$key])"/></Sum> <!-- sum of all same LedgerAccounts / get each Sum-Tag, which has a sibling with the current LedgerAccount (key) -->
          </EachUniqueCombo>
     </xsl:for-each-group>
  </xsl:for-each>

</RegionRow>

  </xsl:template>
</xsl:stylesheet>

XML-Output for our Sample for the first

<?xml version="1.0" encoding="UTF-8"?>
<RegionRow>
    <EachUniqueCombo>
        <key>20000</key>
        <Sum>-516</Sum>
    </EachUniqueCombo>
    <EachUniqueCombo>
        <key>20100</key>
        <Sum>110</Sum>
    </EachUniqueCombo>
    <EachUniqueCombo>
        <key>24000</key>
        <Sum>328.32</Sum>
    </EachUniqueCombo>
    <EachUniqueCombo>
        <key>40000</key>
        <Sum>-43</Sum>
    </EachUniqueCombo>
    <EachUniqueCombo>
        <key>40010</key>
        <Sum>-845.58</Sum>
    </EachUniqueCombo>
    <EachUniqueCombo>
        <key>50000</key>
        <Sum>-570</Sum>
    </EachUniqueCombo>
    <EachUniqueCombo>
        <key>50100</key>
        <Sum>-110</Sum>
    </EachUniqueCombo>
    <EachUniqueCombo>
        <key>51000</key>
        <Sum>-3.3499999999999996</Sum>
    </EachUniqueCombo>
</RegionRow>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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