xsl/xml sub total question

E

eric

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
 
P

Peter Gerstbach

eric said:
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
 
J

Joris Gillis

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:eek:utput 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,
 
E

eric

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
 
J

Joris Gillis

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top