Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sum values - but only if from latest date 1

Status
Not open for further replies.

roadcone20

Programmer
Jan 5, 2004
16
0
0
US
Hi All -

I'm attempting to sum up some values in XML using an XSL transformation. I'm having some trouble, as I'm really new to XSL.

Here's an example of the potential XML:

Code:
<PurchaseOrderLineItems>
  <PurchaseOrderLineItem>
     <Quantity>2</Quantity>
     <Price>50.00</Price>
     <GoodsReceivedNoteItems>
       <GoodsReceivedNoteItem>
          <ReceiptDate>2007-07-02 00:00:00</ReceiptDate>
          <Delivered>
             <Quantity>1</Quantity>
          </Delivered>
       </GoodsReceivedNoteItem>
     <GoodsReceivedNoteItems>
  </PurchaseOrderLineItem>
  <PurchaseOrderLineItem>
     <Quantity>3</Quantity>
      <Price>25.00</Price>
      <GoodsReceivedNoteItems>
        <GoodsReceivedNoteItem>
          <ReceiptDate>2007-07-01 00:00:00</ReceiptDate>
          <Delivered>
              <Quantity>1</Quantity>
          </Delivered>
        </GoodsReceivedNoteItem>
        <GoodsReceivedNoteItem>
          <ReceiptDate>2007-07-02 00:00:00</ReceiptDate>
          <Delivered>
              <Quantity>2</Quantity>
          </Delivered>
       </GoodsReceivedNoteItem>
    </GoodsReceivedNoteItems>
  </PurchaseOrderLineItem>
</PurchaseOrderLineItems>

What I would like to do is sum the Delivered/Quantity values for all PurchaseOrderLineItem/GoodsReceivedNoteItem/GoodsReceivedNoteItem, that are from the latest date in <ReceiptDate>.

So in the example above, the latest date in <ReceiptDate is 2007-07-02, so I want to sum all values in Delivered/Quantity that have this date.

I can find the last date, however, I'm having trouble using that to sum the values.

A push in the right direction would be GREATLY appreciated. Thanks!

 
Sorry, here's my existing xsl. Like I said, I've only been able to determine the last date. Not sure where to take it from there, or if there's a better way to handle what I'm trying to do.

Thanks!

Code:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.1">
	<xsl:output method="xml" indent="yes"/>
	<xsl:call-template name="root"/>
	<xsl:template name="root" match="/PurchaseOrderLineItems">
		<xsl:for-each select="descendant::PurchaseOrderLineItem/GoodsReceivedNoteItems/GoodsReceivedNoteItem/ReceiptDate">
			<xsl:sort/>
			
			<xsl:if test="position() = 1">
				First date: <xsl:value-of select="."/>
			</xsl:if>
			
			<xsl:if test="position() = last() ">
				Last date: <xsl:value-of select="."/>
			</xsl:if>
		</xsl:for-each>
		
	</xsl:template>
</xsl:stylesheet>
 
Okay, my first observation is that last date is not identical to latest date. Do you want the last date (in document order) or do you want the latest date, by calendrical value?

Tom Morrison
 
Ultimately what I want is the latest date by calendrical value. In order to find what the latest date was here, I've placed <xsl:sort/> to sort on the ReceiptDate node and then taking the last value, which should be the latest date. (At least, this is what I think[/] I've done - it seems like it's correct) Now my problem is finding all of the nodes that have this date so that I can sum their Delivered/Quantity values.

I really appreciate your help on this.
 
Okay, this will build on what you have already done. Use a variable to retain the latest date, then use an XPath predicate to select the nodes to be summed.
Code:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]

<xsl:template match="/">
	<xsl:variable name="latestDate">
	    <xsl:for-each select="//ReceiptDate">
		<xsl:sort order="descending"/>
		<xsl:if test="position() = 1"><xsl:value-of select="."/></xsl:if>
		</xsl:for-each>
	</xsl:variable>
	<root><xsl:value-of select="sum(/PurchaseOrderLineItems/PurchaseOrderLineItem/GoodsReceivedNoteItems/GoodsReceivedNoteItem[ReceiptDate = $latestDate]/Delivered)"/></root>
</xsl:template>

</xsl:stylesheet>

Tom Morrison
 
THANK YOU!

That works perfectly - and makes sense. I really appreciate your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top