juls_pro_37: XSLT 1.0 duplikate entfernen und berechnen

Hallo,

ich habe in meinem XML-File mehrere Artikel, welche durch folg. Kriterien zusammengefasst werden sollen (Item.SupplierArticleNumber, Item.FixedPriceGroupID und Item.SalesOriginId = 'PES_CNT'

Im Haupt-XML befinden sich 5 Arikel, Endergebnis sollen es aber nur 3 Artikel sein, da die Item.FixedPriceGroupID 3 verschiedene Werte beinhaltet.

Wie man sieht, habe ich auch unter Item.LineText an sich ebenfalls Vervielfachungen, welche aufgrund von LineText.Qualifier und LineText.Text eindeutig sein sollen.

und als dritten Punkt muss auch noch der Item.SalesPrice aller summierten Artikel zusammengezählt werden. (Beispiel: LineNum 1 = 3,63€ + LineNum 2 = 0,19€ somit muss der SalesPrice 3,82€ ergeben)

falsches XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<SALESINVOICE>
  <Interchange>
    <Recipient>test</Recipient>
    <Sender>test1</Sender>   
  </Interchange>
  <HeaderInformation>
    <OrigInvoiceNumber>1</OrigInvoiceNumber>
    <InvoiceType>INVOIC</InvoiceType>
    <InvoiceDate>2022-10-20</InvoiceDate>
    <InvoiceNumber>test</InvoiceNumber>
  </HeaderInformation>
  <LineInformation>
    <Item>
      <LineNum>1</LineNum>
      <GTIN>12324324324324</GTIN>
      <SupplierArticleNumber>P0000000</SupplierArticleNumber>
      <FixedPrice>381.3500</FixedPrice>
      <SalesPrice>3.63</SalesPrice>
      <SalesOriginId>PES_CNT</SalesOriginId>
      <FixedPriceGroupID>1</FixedPriceGroupID>
      <FixedPriceGroupDescription>test1</FixedPriceGroupDescription>
      <LineText>
        <Qualifier>INV</Qualifier>
        <Text>Text5</Text>
      </LineText>
	        <LineText>
        <Qualifier>INV</Qualifier>
        <Text>Text55</Text>
      </LineText>
	        <LineText>
        <Qualifier>INV</Qualifier>
        <Text>Text5</Text>
      </LineText>
	        <LineText>
        <Qualifier>INV</Qualifier>
        <Text>Text55</Text>
      </LineText>
    </Item>
    <Item>
      <LineNum>2</LineNum>
      <GTIN>56546546546546546546</GTIN>      
      <SupplierArticleNumber>P0000000</SupplierArticleNumber>
      <TotalQuantity>100.00</TotalQuantity>
      <SalesPrice>0.19</SalesPrice>
      <SalesOriginId>PES_CNT</SalesOriginId>
      <FixedPriceGroupID>1</FixedPriceGroupID>
      <FixedPriceGroupDescription>test2</FixedPriceGroupDescription>
    </Item>
    <Item>
      <LineNum>3</LineNum>
      <GTIN>657777657657567</GTIN>      
      <SupplierArticleNumber>P0000000</SupplierArticleNumber>
      <TotalQuantity>100.00</TotalQuantity>
      <SalesPrice>0.08</SalesPrice>
      <SalesOriginId>PES_CNT</SalesOriginId>
      <FixedPriceGroupID>2</FixedPriceGroupID>
      <FixedPriceGroupDescription>test3</FixedPriceGroupDescription>
      <LineText>
        <Qualifier>INV</Qualifier>
        <Text>Text3</Text>
      </LineText>
	        <LineText>
        <Qualifier>INV</Qualifier>
        <Text>Text33</Text>
      </LineText>
	        <LineText>
        <Qualifier>INV</Qualifier>
        <Text>Text3</Text>
      </LineText>
	        <LineText>
        <Qualifier>INV</Qualifier>
        <Text>Text33</Text>
      </LineText>
    </Item>
    <Item>
      <LineNum>4</LineNum>
      <GTIN>22222222222</GTIN>      
      <SupplierArticleNumber>P0000000</SupplierArticleNumber>
      <TotalQuantity>100.00</TotalQuantity>
      <SalesPrice>0.51</SalesPrice>
      <SalesOriginId>PES_CNT</SalesOriginId>
      <FixedPriceGroupID>2</FixedPriceGroupID>
      <FixedPriceGroupDescription>test4</FixedPriceGroupDescription>
    </Item>
  </LineInformation>
</SALESINVOICE>

XSLT:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
    
    <xsl:strip-space elements="*" />
    
    <xsl:key name="header_text" match="HeaderText" use="Text"/>
    <xsl:key name="line_text" match="LineText" use="concat(../LineNum, '|', Text)"/>
    <xsl:key name="line_text2" match="LineText" use="concat(../LineNum, '|', Text)"/>
    <xsl:key name="item" match="Item" use="concat(LineNum, '|', GTIN, '|', Ordered, '|', LineNumSalesLine)"/> 
    <xsl:key name="Item" match="Item" use="concat(SupplierArticleNumber, '|', FixedPriceGroupID, '|', SalesOriginId = 'PES_CNT')"/>
    
    <!-- Identity-Template für die nicht explizit benannten Elemente -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    
    <!-- delete duplicated items -->
    <xsl:template match="LineInformation">      
        
        <xsl:for-each select="//Item[generate-id() = generate-id(key('Item', concat(SupplierArticleNumber, '|', FixedPriceGroupID, '|', SalesOriginId = 'PES_CNT'))[1] )]">
            <xsl:choose>        
                <xsl:when test="//Item[generate-id() = generate-id(key('Item', concat(SupplierArticleNumber, '|', FixedPriceGroupID, '|', SalesOriginId = 'PES_CNT'))[1])]">   
                    <LineInformation>		
                        <Item>
                            <xsl:copy-of select="LineNum"/>
                            <xsl:copy-of select="GTIN"/>
                            <xsl:copy-of select="GTINDescription"/>
                            <xsl:copy-of select="SupplierArticleNumber"/>
                            <xsl:copy-of select="TotalQuantity"/>
                            <xsl:copy-of select="PriceUnit"/>
                            <xsl:copy-of select="FixedPrice"/>																		
                            <SalesPrice>
                                <xsl:copy-of select="sum(key('Item', concat(SupplierArticleNumber, '|', FixedPriceGroupID, '|', SalesOriginId = 'PES_CNT'))[1]/SalesPrice)"/>                               
                            </SalesPrice>
                            <xsl:copy-of select="DiscAmount"/>
                            <xsl:copy-of select="DiscPercent"/>
                            <xsl:copy-of select="SalesLinePercent1"/>
                            <xsl:copy-of select="SalesLinePercent2"/>
                            <xsl:copy-of select="SalesMarkup"/>
                            <xsl:copy-of select="MultiLnDisc"/>
                            <xsl:copy-of select="MultiLnPercent"/>
                            <LineAmount>
                                <xsl:value-of select="sum(key('Item', SupplierArticleNumber)/LineAmount)"/>
                            </LineAmount>
                            <xsl:copy-of select="LineDisc"/>
                            <xsl:copy-of select="SumLineDisc"/>
                            <VatBaseAmount>
                                <xsl:value-of select="sum(key('Item', SupplierArticleNumber)/VatBaseAmount)"/>
                            </VatBaseAmount>
                            <xsl:copy-of select="VatPercentage"/>
                            <xsl:copy-of select="Measure_Unit"/>
                            <xsl:copy-of select="NetWeight"/>
                            <xsl:copy-of select="TaraWeight"/>
                            <xsl:copy-of select="UnitOfWeight"/>
                            <xsl:copy-of select="LineNumSalesLine"/>
                            <xsl:copy-of select="SalesOriginId"/>
                            <xsl:copy-of select="FixedPriceGroupID"/>
                            <xsl:copy-of select="FixedPriceGroupDescription"/>
                            <!-- add line text	-->
                            
                            <xsl:copy-of select="LineText[generate-id() != generate-id(key('line_text2', concat(../LineNum, '|', Text))[1])]"/>      
                            
                            
                        </Item>
                    </LineInformation>
                </xsl:when>            
                <xsl:otherwise>                 
                    <xsl:copy>
                        <xsl:apply-templates select="@* | node()"/>
                    </xsl:copy>                    
                </xsl:otherwise>                
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>
    
    <!-- delete empty nodes -->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="HeaderText[generate-id() != generate-id(key('header_text', Text)[1])]" />
    <xsl:template match="LineText[generate-id() != generate-id(key('line_text', concat(../LineNum, '|', Qualifier, '|', Text))[1])]" />   
    <xsl:template match="Item[generate-id() != generate-id(key('item', concat(LineNum, '|', GTIN, '|', Ordered, '|', LineNumSalesLine))[1])]" />
    
    <xsl:template match="*[not(@*|*|comment()|processing-instruction()) and normalize-space()='']"/>
    
</xsl:stylesheet>

Richtiges XML:

<?xml version="1.0" encoding="UTF-8"?>
<SALESINVOICE>
   <Interchange>
      <Recipient>test</Recipient>
      <Sender>test1</Sender>
   </Interchange>
   <HeaderInformation>
      <OrigInvoiceNumber>1</OrigInvoiceNumber>
      <InvoiceType>INVOIC</InvoiceType>
      <InvoiceDate>2022-10-20</InvoiceDate>
      <InvoiceNumber>test</InvoiceNumber>
   </HeaderInformation>
   <LineInformation>
      <Item>
         <LineNum>1</LineNum>
         <GTIN>12324324324324</GTIN>
         <SupplierArticleNumber>P0000000</SupplierArticleNumber>
         <TotalQuantity>100.00</TotalQuantity>
         <SalesPrice>3.82</SalesPrice>
         <SalesOriginId>PES_CNT</SalesOriginId>
         <FixedPriceGroupID>1</FixedPriceGroupID>
         <FixedPriceGroupDescription>test1</FixedPriceGroupDescription>
         <LineText>
            <Qualifier>INV</Qualifier>
            <Text>Text5</Text>
         </LineText>
         <LineText>
            <Qualifier>INV</Qualifier>
            <Text>Text55</Text>
         </LineText>
      </Item>
      <Item>
         <LineNum>3</LineNum>
         <GTIN>657777657657567</GTIN>
         <SupplierArticleNumber>P0000000</SupplierArticleNumber>
         <TotalQuantity>100.00</TotalQuantity>
         <SalesPrice>0</SalesPrice>
         <SalesOriginId>PES_CNT</SalesOriginId>
         <FixedPriceGroupID>2</FixedPriceGroupID>
         <FixedPriceGroupDescription>test3</FixedPriceGroupDescription>
         <LineText>
            <Qualifier>INV</Qualifier>
            <Text>Text3</Text>
         </LineText>
         <LineText>
            <Qualifier>INV</Qualifier>
            <Text>Text33</Text>
         </LineText>
      </Item>
   </LineInformation>
</SALESINVOICE>

Danke & LG Julian