473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Selecting Nodes Using Subtotal of Child Nodes

I'm having difficulty finding the correct syntax that will allow me to
select a group of invoices based on the total of an amount column
located in its line items. Below are simplified examples of my XML and
XSLT files:

XML FILE

<?xml version="1.0" standalone="yes"?>
<?xml-stylesheet type="text/xsl"
href="OutstandingInvoiceBalances.xslt"?>
<ProgramData>
<Invoices>
<InvoiceID>1</InvoiceID>
<InvoiceNumber>100</InvoiceNumber>
<Amount>1000.00</Amount>
</Invoices>
<Invoices>
<InvoiceID>2</InvoiceID>
<InvoiceNumber>101</InvoiceNumber>
<Amount>2000.00</Amount>
</Invoices>
<Invoices>
<InvoiceID>3</InvoiceID>
<InvoiceNumber>102</InvoiceNumber>
<Amount>3000.00</Amount>
</Invoices>
<InvoiceLineItems>
<InvoiceLineItemID>1</InvoiceLineItemID>
<InvoiceID>1</InvoiceID>
<AmountToPay>0</AmountToPay>
</InvoiceLineItems>
<InvoiceLineItems>
<InvoiceLineItemID>2</InvoiceLineItemID>
<InvoiceID>2</InvoiceID>
<AmountToPay>100</AmountToPay>
</InvoiceLineItems>
<InvoiceLineItems>
<InvoiceLineItemID>3</InvoiceLineItemID>
<InvoiceID>2</InvoiceID>
<AmountToPay>200</AmountToPay>
</InvoiceLineItems>
<InvoiceLineItems>
<InvoiceLineItemID>4</InvoiceLineItemID>
<InvoiceID>3</InvoiceID>
<AmountToPay>100</AmountToPay>
</InvoiceLineItems>
<InvoiceLineItems>
<InvoiceLineItemID>5</InvoiceLineItemID>
<InvoiceID>3</InvoiceID>
<AmountToPay>200</AmountToPay>
</InvoiceLineItems>
<InvoiceLineItems>
<InvoiceLineItemID>6</InvoiceLineItemID>
<InvoiceID>3</InvoiceID>
<AmountToPay>300</AmountToPay>
</InvoiceLineItems>
</ProgramData>
XSLT FILE

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/TR/REC-html40"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:local="#local-functions">
<xsl:template match="/">
<HTML>
<BODY>
<TABLE CELLSPACING="1" CELLPADDING="1" BORDER="0">
<TR>
<TH>Invoice Number</TH>
<TH width="10"></TH>
<TH align="right">Amount</TH>
<TH width="10"></TH>
<TH align="right">Amount Paid</TH>
</TR>
<TR>
<TD colspan="17" height="1" bgcolor="black"></TD>
</TR>

<xsl:variable name="SelectData" select="ProgramData/Invoices"
/>
<xsl:for-each select="$SelectData">

<xsl:variable name="AmountToPay"
select="sum(//InvoiceLineItems[InvoiceID=current()/InvoiceID]/AmountToPay)"
/>
<TR>
<TD><xsl:value-of select="InvoiceNumber" /></TD>
<TD></TD>
<TD align="right"><xsl:value-of
select="format-number(Amount, '#,##0.00')" /></TD>
<TD></TD>
<TD align="right"><xsl:value-of
select="format-number($AmountToPay, '#,##0.00')" /></TD>
</TR>

</xsl:for-each>

<TR>
<TD colspan="17" height="1" bgcolor="black"></TD>
</TR>
<xsl:variable name="GrandAmount"
select="sum($SelectData/Amount)" />
<xsl:variable name="GrandAmountToPay"
select="sum(ProgramData/InvoiceLineItems[InvoiceID=$SelectData/InvoiceID]/AmountToPay)"
/>
<TR>
<TD>Grand Total</TD>
<TD></TD>
<TD align="right"><xsl:value-of
select="format-number($GrandAmount, '#,##0.00')" /></TD>
<TD></TD>
<TD align="right"><xsl:value-of
select="format-number($GrandAmountToPay, '#,##0.00')" /></TD>
</TR>

</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
When I run the translation I get the following result:

Invoice Number Amount Amount Paid
100 1,000.00 0.00
101 2,000.00 300.00
102 3,000.00 600.00
Grand Total 6,000.00 900.00

As you can see from the script each invoice's Amount Paid value is
calculated as the sum of its line item Amount Paid values. What I
would like to do is only display those invoices where the Amount Paid
is greater than 0. I realize I could wrap the code contained within
the for-each loop with a test to ensure the the sum is greater than 0
before writing the value but that doesn't help the Grand Total row. It
would still include all invoices in its summation. Using the example
above, the Grand Total of the Amount column would still be 6,000.00
despite the fact that invoice 100 would not be displayed.

What I've tried to do is exclude those invoices from the $SelectData
variable. If I can do that the entire sheet would calculate correctly:

Invoice Number Amount Amount Paid
101 2,000.00 300.00
102 3,000.00 600.00
Grand Total 5,000.00 900.00

I've tried everything I can think of to accomplish this to no avail.
If anyone can point me in the right direction I would appreciate it.

Thanks

Aug 23 '06 #1
1 1798
re*********@hotmail.com wrote:
I'm having difficulty finding the correct syntax that will allow me to
select a group of invoices based on the total of an amount column
located in its line items. Below are simplified examples of my XML and
XSLT files:
.....
When I run the translation I get the following result:

Invoice Number Amount Amount Paid
100 1,000.00 0.00
101 2,000.00 300.00
102 3,000.00 600.00
Grand Total 6,000.00 900.00

As you can see from the script each invoice's Amount Paid value is
calculated as the sum of its line item Amount Paid values. What I
would like to do is only display those invoices where the Amount Paid
is greater than 0.
Wait -- I am confused about the things that are named AmountToPay, that
you call amount paid??? As far as I know, that something is to pay means
is not yet paid?
I realize I could wrap the code contained within
the for-each loop with a test to ensure the the sum is greater than 0
before writing the value but that doesn't help the Grand Total row. It
would still include all invoices in its summation. Using the example
above, the Grand Total of the Amount column would still be 6,000.00
despite the fact that invoice 100 would not be displayed.

What I've tried to do is exclude those invoices from the $SelectData
variable. If I can do that the entire sheet would calculate correctly:

Invoice Number Amount Amount Paid
101 2,000.00 300.00
102 3,000.00 600.00
Grand Total 5,000.00 900.00

I've tried everything I can think of to accomplish this to no avail.
If anyone can point me in the right direction I would appreciate it.
Try a key: The 'gedefims' one I included rounds up a map from each
InvoiceID to lists of InvoiceLineItems with the ID. That is then used
later in a predicate, where the text value of the InvoiceID children are
used for looking up the lists. The list nodes' AmountToPay children are
summed, and if the result 0, the Amount child of the parent of
InvoiceID's parent is included in the grand total...........

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/TR/REC-html40"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:local="#local-functions" version="1.0">
<xsl:key name="gedefims" match="InvoiceLineItems" use="InvoiceID"/>
<xsl:template match="/">
<HTML>
<BODY>
<TABLE CELLSPACING="1" CELLPADDING="1" BORDER="0">
<TR>
<TH>Invoice Number</TH>
<TH width="10"/>
<TH align="right">Amount</TH>
<TH width="10"/>
<TH align="right">Amount Paid</TH>
</TR>
<TR>
<TD colspan="17" height="1" bgcolor="black"/>
</TR>
<xsl:variable name="SelectData" select="ProgramData/Invoices"/>
<xsl:for-each select="$SelectData">
<xsl:variable name="AmountToPay"
select="sum(//InvoiceLineItems[InvoiceID=current()/InvoiceID]/AmountToPay)"/>
<xsl:if test="$AmountToPay &gt; 0">
<TR>
<TD>
<xsl:value-of select="InvoiceNumber"/>
</TD>
<TD/>
<TD align="right">
<xsl:value-of select="format-number(Amount,
'#,##0.00')"/>
</TD>
<TD/>
<TD align="right">
<xsl:value-of select="format-number($AmountToPay,
'#,##0.00')"/>
</TD>
</TR>
</xsl:if>
</xsl:for-each>
<TR>
<TD colspan="17" height="1" bgcolor="black"/>
</TR>
<xsl:variable name="GrandAmount"
select="sum(/ProgramData/Invoices [sum(key('gedefims',
InvoiceID)/AmountToPay) &gt; 0]/Amount)"/>
<xsl:variable name="GrandAmountToPay"
select="sum(ProgramData/InvoiceLineItems[InvoiceID=$SelectData/InvoiceID]/AmountToPay)"/>
<TR>
<TD>Grand Total</TD>
<TD/>
<TD align="right">
<xsl:value-of select="format-number($GrandAmount,
'#,##0.00')"/>
</TD>
<TD/>
<TD align="right">
<xsl:value-of select="format-number($GrandAmountToPay,
'#,##0.00')"/>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>

Hope that confused --- uh, helped,

Søren
Aug 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jamie Green | last post by:
Using MSXML3.0, with the Dom SelectionLanguage set to Xpath I am trying to query the following document <Root> <Child>Name</Child> <Child>John</Child> <Child>Smith</Child> <Child>23</Child>...
0
by: J. T. | last post by:
I'm fairly new to XSL, but have made some good progress in picking it up for the project I'm currently working on. I've been trying to figure out how to design a stylesheet that would transform...
9
by: Tjerk Wolterink | last post by:
Hello, suppose i have a dom like this: <a> - <b> - <b> - <d> - </b> - <c>
2
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this...
1
by: Nancy Shelley | last post by:
Hi all: I am building a navigation menu using telerik's rad treeview I am able to build the outer menu but not the children. How do I select the child nodes (item) from within the loop? Any...
2
by: Saurabh Sharma | last post by:
Hi, I am using Dom Parsing in Xml. I am the parent node and it has many children and each children has many children. I want to select children with a given name . Is there any method by which we...
2
by: Marc Jennings | last post by:
Hi there, I have been given a rather poor schema for an XML file in the following format : ><item> > <key>ProductCode</key> > <value>1234-5678</value> > <key>Description</key> >...
2
by: Tymbow | last post by:
I'm building a web application that is analogous to the Windows XP file explorer in function. The left column contains a TreeView, and the right column a DataGrid populated by selecting TreeView...
1
by: jdhcards | last post by:
Hello, I've been banging my head against a problem all day without a solution, and I'm hoping you all can help. I've got a piece of XML that defines a set of elements in a flat list. Each of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.