How to form a query for selection based on the value of an element

We have an xml document with invoices / [asst. elements], using the xslt call pattern, we need to summarize InvoiceAmounts to match InvoiceNumbers.

Enter the xml file:

    <Invoices>   
     <Invoice>  
      <InvoiceNumber>351510</InvoiceNumber> 
      <InvoiceAmount>137.50</InvoiceAmount> 
     </Invoice>   
     <Invoice>  
      <InvoiceNumber>351510</InvoiceNumber> 
      <InvoiceAmount>362.50</InvoiceAmount> 
     </Invoice>   
     <Invoice>  
      <InvoiceNumber>351511</InvoiceNumber> 
      <InvoiceAmount>239.50</InvoiceAmount> 
     </Invoice>  
    </Invoices>

I found a select statement that returns the total amount of InvoiceAmounts for the entire document, but I need to filter it to the amount based on InvoiceNumber, since there can be more than one invoice with the same InvoiceNumber in the invoice document.

This returns the total amount:

<xsl:value-of select="sum(/*[local-name()='Invoices']/*[local-name()='Invoice']/*[local-name()='InvoiceAmount'])" />

, -, . , p1, . , InvoiceNumbers = $p1? , . $p1 ?

<xsl:value-of select="sum(/*[local-name()='Invoices']/*[local-name()='Invoice'][local-name()='InvoiceAmount' = $p1]/*[local-name()='InvoiceAmount'])" />

:

<Invoices>
 <Invoice>
  <InvoiceNumber>351510</InvoiceNumber>
  <InvoiceAmount>500.00</InvoiceAmount>
 </Invoice>
 <Invoice>
  <InvoiceNumber>351510</InvoiceNumber>
  <InvoiceAmount>500.00</InvoiceAmount>
 </Invoice>
 <Invoice>
  <InvoiceNumber>351511</InvoiceNumber>
  <InvoiceAmount>239.50</InvoiceAmount>
 </Invoice>
</Invoices>

.

+3
2

:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="InvoiceAmount">
        <InvoiceAmount>
            <xsl:value-of 
               select="sum(../../Invoice[InvoiceNumber=
                   current()/parent::*/InvoiceNumber]/InvoiceAmount)"/>
       </InvoiceAmount>
    </xsl:template>
</xsl:stylesheet>

:

<Invoices>   
     <Invoice>  
         <InvoiceNumber>351510</InvoiceNumber> 
         <InvoiceAmount>500</InvoiceAmount> 
     </Invoice>   
     <Invoice>  
         <InvoiceNumber>351510</InvoiceNumber> 
         <InvoiceAmount>500</InvoiceAmount> 
     </Invoice>   
     <Invoice>  
         <InvoiceNumber>351511</InvoiceNumber> 
         <InvoiceAmount>239.5</InvoiceAmount> 
     </Invoice>  
</Invoices>

. . . xsl:decimal-format.

+1

, - XML-:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kInvAmmtByNumber" match="InvoiceAmount"
  use="../InvoiceNumber"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="InvoiceAmount/text()">
  <xsl:value-of select=
  "sum(key('kInvAmmtByNumber', ../../InvoiceNumber))"/>
 </xsl:template>
</xsl:stylesheet>

XML-:

<Invoices>
    <Invoice>
        <InvoiceNumber>351510</InvoiceNumber>
        <InvoiceAmount>137.50</InvoiceAmount>
    </Invoice>
    <Invoice>
        <InvoiceNumber>351510</InvoiceNumber>
        <InvoiceAmount>362.50</InvoiceAmount>
    </Invoice>
    <Invoice>
        <InvoiceNumber>351511</InvoiceNumber>
        <InvoiceAmount>239.50</InvoiceAmount>
    </Invoice>
</Invoices>

, :

<Invoices>
   <Invoice>
      <InvoiceNumber>351510</InvoiceNumber>
      <InvoiceAmount>500</InvoiceAmount>
   </Invoice>
   <Invoice>
      <InvoiceNumber>351510</InvoiceNumber>
      <InvoiceAmount>500</InvoiceAmount>
   </Invoice>
   <Invoice>
      <InvoiceNumber>351511</InvoiceNumber>
      <InvoiceAmount>239.5</InvoiceAmount>
   </Invoice>
</Invoices>
0

All Articles