473,686 Members | 2,753 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

xsl/xml sub total question

Hi,everyone:

I have my xml data defined as

xml data definition I:

<?xml version="1.0"?>
<parents>
<parent>
<id>1000</id>
<name> p1 </name>
<premium> 1000</premium>
<loss> 500 </loss>
<ratio> 50% </ratio>
<child>
<id>1001</id>
<name> c1 </name>
<premium> 2000</premium>
<loss> 500 </loss>
<ratio> 25% </ratio>
</child>

<child>
<id>1002</id>
<name> c2 </name>
<premium> 3000</premium>
<loss> 900 </loss>
<ratio> 30%</ratio>
</child>
</parent>

<parent>

<id>2000</id>
<name> p2 </name>
<premium> 4000</premium>
<loss> 800 </loss>
<ratio> 20%</ratio>
<child>
<id>2001</id>
<name> c3 </name>
<premium> 5000</premium>
<loss> 5000 </loss>
<ratio> 100%</ratio>
</child>

</parent>

<parent>

<id>3000</id>
<name> p3 </name>
<premium> 1000</premium>
<loss> 1500 </loss>
<ratio> 150%</ratio>
</parent>

</parents>

--------------------------------------------------------------------------------
xml definition II:
<?xml version="1.0"?>
<parents>
<parent id=1000>
<child>
<id>1000</id>
<name> p1 </name>
<premium> 1000</premium>
<loss> 500 </loss>
<ratio> 50% </ratio>
</child>

<child>
<id>1001</id>
<name> c1 </name>
<premium> 2000</premium>
<loss> 500 </loss>
<ratio> 25% </ratio>
</child>

<child>
<id>1002</id>
<name> c2 </name>
<premium> 3000</premium>
<loss> 900 </loss>
<ratio> 30% </ratio>
</child>
</parent>

<parent id=2000>

<child>
<id>2000</id>
<name> p2 </name>
<premium> 4000</premium>
<loss> 800 </loss>
<ratio> 20% </ratio>
</child>

<child>
<id>2001</id>
<name> c3 </name>
<premium> 5000</premium>
<loss> 5000 </loss>
<ratio> 100% </ratio>
</child>

</parent>

<parent id=3000>
<child>
<id>3000</id>
<name> p3 </name>
<premium> 1000</premium>
<loss> 1500 </loss>
<ratio> 150% </ratio>
</child>
</parent>

</parents>

------------------------------------------------------------------------------
I would like to get some output(e.g.:htm l) like following with my xml
data through xsl transformation.

Parent Child Premium Loss Loss ratio
ID ID
1000 1000 1000 500 50%
1001 2000 500 25%
1002 3000 900 30%
subtotal 6000 1900 31.67%

2000 2000 4000 800 50%
2001 5000 5000 100%
subtotal 9000 5800 64.44%

3000 3000 1000 1500 150%
subtotal 1000 1500 150%
total 16000 9200 57.50%

I am wondering which xml definition is easier to write xsl to get this
subtotal and total.

thanks!

Eric
Jul 20 '05 #1
5 4081
eric wrote:
I am wondering which xml definition is easier to write xsl to get this
subtotal and total.

Well, in my opinion there is not really a big difference to write the
XSL between these two definitions. With XPath you can address the right
elements anyway to build the totals.

The question (how to write the XML) should be, which definition better
corresponds to the real world.
Peter
Jul 20 '05 #2
Hi,Joris:
Can you help me on that??
Thanks

Eric
Jul 20 '05 #3
Hi,
I am wondering which xml definition is easier to write xsl to get this
subtotal and total.

Hi,Joris:
Can you help me on that??


Someone explicitly calling my aid?? I'm sure most readers of this newsgroup could help you too :)

If those 'id' elements really contain (unique) id's, you'd better make them attributes (like in your second definition, but also for the children). And don't forget to quote all atrribute values :).
Like Peter Gerstbach said, it's no noticeable difference for the xslt, but semantically, it makes more sense.
Also, I wouldn't call the container elements 'child' or 'parent(s)': the xml structure itself already expresses the relationship beteween the elements.
Finally, I would omit the 'ratio' element, since it's value can easily be calculated with the other elements.

The following xslt will output a html table from your first XML definition.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="html" indent="yes" />

<xsl:template match="/">
<html>
<head/>
<body>
<xsl:apply-templates select="parents "/>
</body>
</html>
</xsl:template>

<xsl:template match="parents" >
<table>
<thead>
<th>Parent ID</th><th>Child ID</th><th>Premium</th><th>Loss</th><th>Loss ratio</th>
</thead>
<tbody>
<xsl:apply-templates select="parent"/>
<xsl:call-template name="totals"/>
</tbody>
</table>
</xsl:template>

<xsl:template match="parent">
<xsl:apply-templates select=".|child " mode="fillRow"/>
<xsl:call-template name="totals"/>
</xsl:template>

<xsl:template name="totals">
<tr>
<td/>
<td><xsl:if test="self::par ent">sub</xsl:if>total</td>
<td><xsl:valu e-of select="sum(.//premium)"/></td>
<td><xsl:valu e-of select="sum(.//loss)"/></td>
<td><xsl:valu e-of select="format-number(sum(.//loss) div sum(.//premium), '###.##%')"/></td>
</tr>
</xsl:template>

<xsl:template match="*" mode="fillRow">
<tr>
<td><xsl:if test="self::par ent"><xsl:valu e-of select="id"/></xsl:if></td>
<td><xsl:valu e-of select="id"/></td>
<td><xsl:valu e-of select="premium "/></td>
<td><xsl:valu e-of select="loss"/></td>
<td><xsl:valu e-of select="ratio"/></td>
</tr>
</xsl:template>

</xsl:stylesheet>

regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #4
Hi,
In my real data, some premium or loss value from a parentID or childID
are missing which results subtotal or total has a NaN value.

I try something like

sum(b/data[.!=''])
sum(/a/b/data[number()=number ()])
<xsl:value-of select="sum(b/data[number(.)=numbe r(.)]"/>

It seems not working. any other ideas??

Thanks
Eric
Jul 20 '05 #5
> In my real data, some premium or loss value from a parentID or childID
are missing which results subtotal or total has a NaN value.

I try something like

sum(b/data[.!=''])
sum(/a/b/data[number()=number ()])
<xsl:value-of select="sum(b/data[number(.)=numbe r(.)]"/>

It seems not working. any other ideas??

this should work: <xsl:value-of select="sum(b/data[boolean(number( ))]"/>
I believe there's a shorter, way, but it seems I don't remember now...

regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #6

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

Similar topics

4
11817
by: James Greig | last post by:
hello people, i'm just learning javascript, could someone point me in the direction of an example of the following, or give me some clues as to how it might be done: what i would like to do is make a self totalling version of this page > http://www.ayrshirehousing.org.uk/applications/applications.html so that basically as a person checked or unchecked boxes, a
1
4887
by: Marie | last post by:
How do you display a total from a subreport on the main report. If it makes a difference, the total is a total of a calculated field (Qty * Price). Does the total on the subreport go in the pagefooter or report footer? Say the total is shown in a texrbox named MyTotal and the subreport control is named MySubreportControl, what exactly do I put in the control source of a textbox on the main report to show the total? Thanks For All Help! ...
2
5336
by: Jon | last post by:
I have a datagrid with a total row on the bottom. What is the best method to "instantly" update the total row when the value of any row changes. I am running into problems when I use the column changed event to calculate the total and assign it directly to the grid -> myDataGridObject=newTotal. When I do this, more change events are fired, causing unwanted consequences.
16
2457
by: ken | last post by:
I have a formA and subformB subformB is a continous form with a txtTotal in form footer =Sum() This works fine as long as there are records in form but if form is null I get Error I would like to get arround this as I Have a label to be Visible only if txtTotal>= 45000
7
1830
lee123
by: lee123 | last post by:
hey all, i have made a order form with all the works. in vb 6 and in this form i have been trying to get a total of the items that a customer would have ordered. to make things make sense i have in my order form ( qty, product,unit price, and a total) there are 12 rows of these text boxes (1-44) i believe, any way on the bottom of the order form there is a (subtotal,salestax,final total.) now the question is.....(and it probably a simple one i...
1
1551
by: damezumari | last post by:
How do I get the total without a condition and the total with the condition at the same time? This is my mysql table: id datetimeconfirmed customerid amount 1 2007-08-14 06:19:03 1 12.25 2 2007-08-06 06:19:19 1 15 3 2007-08-02 06:19:41 2 5 4 2007-08-07 06:19:58 2 7.25
5
3674
by: alanb | last post by:
Hi, hope someone can help, I need to be able to keep a running total of radio buttons selected, as a user goes through a set of 16 questions, devided in to 4 catorgories, then on "submit" have the total number showen on screen, or on "submit" loop through the selected radio buttons and get the total. What I have at the moment will loop through all the buttons and just add all the totals, how do I get this code to add together the...
21
17435
beacon
by: beacon | last post by:
Hello to everybody, I have a section on a form that has 10 questions, numbered 1-10, with 3 option buttons per question. Each of the option buttons have the same response (Yes, No, Don't know), but each answer is given a different point value for each question. For instance, question 1 might ask "Do you like candy?" and if you answer Yes you will get +1, No will earn you -1, and Don't know will net you zero points. The next question might...
6
5916
by: plaguna | last post by:
I’m creating a Microsoft Access Report of 6 different questions with “Yes” and “No” answers. I have no problem to count the Yes and Nos with the following formulas for each question in the Report Footer: =Count(IIf(=”YES”,0)) =Count(IIf(=”NO”,0)) My question is: how can I get a percentage of NOs for each question? I tried to create a new field (Total of Yes) and (Total of No) in my query with the above formulas, that way I can manipulate...
3
1504
by: MyWaterloo | last post by:
I need some help with an inventory type question. To lay the foundation... I have an inventory database that keeps track of parts for rebuilds. The tables are: CATEGORIES which is linked to table PRODUCTS. The PRODUCTS table is the parts needed to rebuild the items in the CATEGORIES table. I have a form where I can pick a category that will open a new form showing all the parts (from table PRODUCTS) that relate to that category (from table...
0
8516
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8932
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8768
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7599
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6440
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4308
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1934
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.