juls_pro_37: XSLT 1.0 Sum

Beitrag lesen

Hi,

bräuchte für den Knoten "Total/AllowanceOrCharge_DEEE" ein neues Segment "Amount" mit der Summe von "Item/TotalQuantity" * "AllowanceOrCharge_Line/Amount" wenn unter "Item/AllowanceOrCharge_Line" der "Code" = "TX".

Sprich (75 * 2,08) + (5*2,08) = 166,40

XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<SALESINVOICE>  
  <HeaderInformation>
    <InvoiceNumber>Test</InvoiceNumber>    
  </HeaderInformation>
  <LineInformation>
    <Item>
      <LineNum>1</LineNum>
      <TotalQuantity>75.00</TotalQuantity>  
      <AllowanceOrCharge_Line>
        <Code>TX</Code>
        <Amount>2.08</Amount>        
      </AllowanceOrCharge_Line>
    </Item>  
    <Item>
      <LineNum>2</LineNum>
      <TotalQuantity>5.00</TotalQuantity>  
      <AllowanceOrCharge_Line>
        <Code>TX</Code>       
        <Amount>2.08</Amount>
      </AllowanceOrCharge_Line>
    </Item>
  </LineInformation> 
  <Totals>
    <AllowanceOrCharge_DEEE>
        <Code>TX</Code>
    </AllowanceOrCharge_DEEE>
  </Totals>
</SALESINVOICE>

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes" indent="yes"/>
  <xsl:strip-space elements="*" />
    
    <xsl:template match="AllowanceOrCharge_DEEE">     
        <xsl:copy>
         <!--copy all other nodes-->
         <xsl:apply-templates select="@* | node()"/>  
          <xsl:for-each select="//Item/AllowanceOrCharge_Line">
            <xsl:if test="./Code = 'TX'">           
            <Amount>
                <xsl:value-of select="sum((./TotalQuantity * ./AllowanceOrCharge_Line/Amount))"/>               
            </Amount>
              </xsl:if>              
        </xsl:for-each> 
        </xsl:copy>
    </xsl:template>

  <!-- Identity-Template für die nicht explizit benannten Elemente -->
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

Korrekt:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<SALESINVOICE>  
  <HeaderInformation>
    <InvoiceNumber>Test</InvoiceNumber>    
  </HeaderInformation>
  <LineInformation>
    <Item>
      <LineNum>1</LineNum>
      <TotalQuantity>75.00</TotalQuantity>  
      <AllowanceOrCharge_Line>
        <Code>TX</Code>
        <Amount>2.08</Amount>        
      </AllowanceOrCharge_Line>
    </Item>  
    <Item>
      <LineNum>2</LineNum>
      <TotalQuantity>5.00</TotalQuantity>  
      <AllowanceOrCharge_Line>
        <Code>TX</Code>       
        <Amount>2.08</Amount>
      </AllowanceOrCharge_Line>
    </Item>
  </LineInformation> 
  <Totals>
    <AllowanceOrCharge_DEEE>
        <Code>TX</Code>
        <Amount>166.40</Amount>
    </AllowanceOrCharge_DEEE>
  </Totals>
</SALESINVOICE>

danke & lg Julian

akzeptierte Antworten