Advanced XML rule example: Summarize data from line items as a note in a summary invoice
Summary invoices may contain multiple line items that you want to summarize in a note at the end of the invoice for ease of processing. This example shows how this can be done.
- Select the InvoiceNote field in the Fields section on the Fields tab of the XML Mapper (on the right side of the window).
- Click the button to the right of the value to display the Field mapping rules for the field on the left.
Use XSL on the bottom left of the screen and
add this example code in the text box below the setting.
This example code loops through the XML to find how many instances are found for each unique value for the <Description> element and then summarizes them all in a note at the end of the invoice. The green text provides information on what each section of the code does. It can be removed.
<xsl:template match="/"> <!-- Get all unique values for ‘Description‘ --> <xsl:for-each select="//cac:InvoiceLine[not(cac:Item/cbc:Description = preceding::cac:Item/cbc:Description)]"> <xsl:sort select="cac:Item/cbc:Description"/> <!-- Sort by Description --> <!-- For each Description --> <xsl:variable name='CurrentPerson' select="cac:Item/cbc:Description" /> <!-- Get value of Description --> <xsl:variable name='NodeForCurrentPerson' select="//cac:InvoiceLine[cac:Item/cbc:Description=$CurrentPerson]" /> <!-- Get node set of Description --> <xsl:variable name='NumOfRows' select="count($NodeForCurrentPerson)" /> <!-- Count number of nodes for Description --> <xsl:variable name='SumOfCost' select="sum($NodeForCurrentPerson/cac:Item/cac:BasePrice/cbc:PriceAmount)" /> <!-- Sum of value of all PriceAmount nodes for Description --> Person: <xsl:value-of select="$CurrentPerson"/> #Rows: <xsl:value-of select="$NumOfRows"/> Totalt:<xsl:value-of select="$SumOfCost"/> <!-- Compose the value of the Note field --> </xsl:for-each> </xsl:template>
- Consider if the change(s) should apply to all invoices or only those for the current XML invoice definition. Click Save XML definitionto save the change for the current XML invoice definition only (that is, the current supplier). Click Save XML profile if you want the rule to apply to all XML invoices that match the XML invoice profile regardless of the XML invoice definition.
Test the rule
Test this rule using the example file provided in the installation: ...\ReadSoft\INVOICES\Samples\XML\SummaryInvoice-Example.xml
- Interpret the file using a job in which a folder is set up as the input source. See "Configuring a file folder as the input source for XML files" in Collector help for more information.
- In Optimize, set up the advanced rule as outlined in steps 1-3 above.
- Click Extract, and then click Save XML definition.
- Click Close and then Yes when asked if you are finished optimizing the XML invoice definition.
- Run the Interpret job again.
Open the invoice in Verify, and this note appears on the last
page of the PDF:
Other examples of advanced XML rules: