Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 22nd, 2007, 09:21 AM
Friend of Wrox
Points: 718, Level: 10
Points: 718, Level: 10 Points: 718, Level: 10 Points: 718, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: Ennis, Clare, Ireland.
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default Calculate a running total

Hi,

Within the following code, There are 2 'OrderLines' which are calculated at the foot of the code in the 'OrderControlTotals/TotalNumberOfLines' element. I need to perform a calculation on the repeated lines that begin 'Quantity QuantityType="Order Quantity" QuantityUOM' to be output in the element 'TotalQuantity' (Shown as 'XXXX'). The current XSLT is displayed after the XML.

[u]XML</u>
Code:
<QQQOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://QQQ.com/QQQXML/Schemas/v3_0_1/QQQOrd.xsd" OrderType="Standalone Order" DocumentType="New">

  <F4FDocumentHeader>
    <SchemaVersion>3</SchemaVersion>
    <SchemaStatus>Approved</SchemaStatus>
    <DocumentCreated DateTimeType="Document Created">20070315 09:32</DocumentCreated>
    <DocumentTrackingId>56709</DocumentTrackingId>
    <DocumentRevisionNumber>1</DocumentRevisionNumber>
    <SourcePartnerID>AT</SourcePartnerID>
    <SourceDivisionID>KA</SourceDivisionID>
    <DestinationPartnerID>ASATST</DestinationPartnerID>
  </F4FDocumentHeader>

  <OrderHeader>
  .... 
  </OrderHeader>
  
  <TransportDetails>
  ....
  </TransportDetails>
 
  <OrderLine LineNumber="1">
    <LineReference ReferenceType="Contract Number" AssignedBy="Buyer">0006509602/01</LineReference>
    <LineReference ReferenceType="Contract Number" AssignedBy="Supplier">ppersupref</LineReference>
    <ProductReference ReferenceType="Assigned By Buyer">496500</ProductReference>
    <ProductDescription Type="Defined by Buyer">REGULATOR 1</ProductDescription>
    <ProductValues>
      <Quantity QuantityType="Order Quantity" QuantityUOM="Unit">1000</Quantity>
      <UnitPrice PriceUOM="Unit">0.59</UnitPrice>
    </ProductValues>
  </OrderLine>

  <OrderLine LineNumber="2">
    <LineReference ReferenceType="Contract Number" AssignedBy="Buyer">0006509602/01</LineReference>
    <LineReference ReferenceType="Contract Number" AssignedBy="Supplier">ppersupref</LineReference>
    <ProductReference ReferenceType="Assigned By Buyer">496700</ProductReference>
    <ProductDescription Type="Defined by Buyer">REGULATOR 2</ProductDescription>
    <ProductValues>
      <Quantity QuantityType="Order Quantity" QuantityUOM="Unit">500</Quantity>
      <UnitPrice PriceUOM="Unit">0.59</UnitPrice>
    </ProductValues>
  </OrderLine>

  <OrderControlTotals>
    <TotalNumberOfLines>2</TotalNumberOfLines>
    <TotalQuantity>XXXX</TotalQuantity>
  </OrderControlTotals>

</QQQOrder>
[u]XSLT</u>
Code:
<xsl:template name="OrderLine">
  <xsl:for-each select="../OrderLine[@LineNumber]">
    <OrderLine LineNumber="{position()}">
      <xsl:element name="ProductValues">
        <Quantity ReferenceType="QuantityType" AssignedBy="Order Quantity">
          <xsl:choose>
            <xsl:when test="ProductReference[@ReferenceType]='496500'">
              <xsl:value-of select="(ProductValues/Quantity[@QuantityType='Order Quantity']) div 100"/>
            </xsl:when>
            <xsl:when test="ProductReference[@ReferenceType]='496700'">
              <xsl:value-of select="(ProductValues/Quantity[@QuantityType='Order Quantity']) div 50"/>
            </xsl:when>
            ....
            etc.
            .... 
            <xsl:otherwise>
              <xsl:value-of select="'UNKNOWN'"/>
            </xsl:otherwise>
          </xsl:choose>
        </Quantity>        
      </xsl:element>
    </OrderLine>            
  </xsl:for-each>
</xsl:template>
So, the line that I need to 'accumulate' (running total) are the results of
Code:
<xsl:value-of select="(ProductValues/Quantity[@QuantityType='Order Quantity']) div XX"/>
Where, 'XX' is specific to the 'ProductReference'. In the XML example above, they are (1000 / 100) and (500 / 50).

(10 + 10 = 20).
Therefore, 'TotalQuantity' should = 20

How do I perform this?

Thanks in advance,


Neal

A Northern Soul
__________________
Neal

A Northern Soul
  #2 (permalink)  
Old March 22nd, 2007, 09:52 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

To do a running total in XSLT you need recursion. Write a template that processes the first item, computes the total so far, and the calls itself to process the rest of the list. Typically you will have two parameters, the list of items to be processed and the total so far, and you terminate the recursion when the list becomes empty.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
  #3 (permalink)  
Old March 22nd, 2007, 10:18 AM
Friend of Wrox
Points: 718, Level: 10
Points: 718, Level: 10 Points: 718, Level: 10 Points: 718, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: Ennis, Clare, Ireland.
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Hi,

I appreciate your thoughts, only I haven't used parameters in XSLT before. How are the values passed?
For example, How do I pass the following?
Code:
(ProductValues/Quantity[@QuantityType='Order Quantity']) div 50
Thanks,

Neal

A Northern Soul
  #4 (permalink)  
Old March 22nd, 2007, 11:16 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

There's a whole chapter on recursive programming in my book. I suggest you read it. Or if you have a different book, look up xsl:param and xsl:with-param in the index

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculate running total on form and prevent text Hughesie78 ASP.NET 2.0 Basics 0 November 27th, 2007 10:35 AM
Calculate total hours based on Time deepcover1 Access 1 April 5th, 2006 01:50 AM
How To Calculate Monthy Total on-the-clock time Michaniker Access 2 December 2nd, 2005 06:06 PM
Running Total jdmorr SQL Language 5 March 9th, 2005 03:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.