472,351 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,351 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 4013
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...
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)....
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. ...
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...
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...
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 ...
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,...
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...
6
by: plaguna | last post by:
Im 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...
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...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

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.