473,385 Members | 1,814 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,385 software developers and data experts.

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.:html) 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 4069
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:stylesheet 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::parent">sub</xsl:if>total</td>
<td><xsl:value-of select="sum(.//premium)"/></td>
<td><xsl:value-of select="sum(.//loss)"/></td>
<td><xsl:value-of select="format-number(sum(.//loss) div sum(.//premium), '###.##%')"/></td>
</tr>
</xsl:template>

<xsl:template match="*" mode="fillRow">
<tr>
<td><xsl:if test="self::parent"><xsl:value-of select="id"/></xsl:if></td>
<td><xsl:value-of select="id"/></td>
<td><xsl:value-of select="premium"/></td>
<td><xsl:value-of select="loss"/></td>
<td><xsl:value-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(.)=number(.)]"/>

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(.)=number(.)]"/>

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
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...
1
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...
2
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...
16
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...
7
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...
1
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 ...
5
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...
21
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),...
6
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...
3
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.