Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Sum over computed value ?

Reply
Thread Tools

Sum over computed value ?

 
 
Thomas Zangl
Guest
Posts: n/a
 
      01-10-2006
Hi!

I have an XML like this:

<?xml version="1.0" encoding="UTF-8"?>
<invoice>
<bill>
<BId>20</BId>
<CId>73</CId>
<BDate>2006-01-10</BDate>
<BStatus>0</BStatus>
</bill>

<billitems>

<item>
<PName>Kiwi</PName>
<PPrice>0.900</PPrice>
<PId>1</PId>
<BCQuant>15</BCQuant>

</item>

<item>
<PName>Apfel</PName>
<PPrice>0.500</PPrice>
<PId>3</PId>
<BCQuant>10</BCQuant>

</item>

</billitems>
</invoice>

Now I want to have the sum of
/invoice/billitems/item/BCQuant*/invoice/billitems/item/PPrice

(=total price)
Any ideas how to do?

TIA!
--
----------------------------------------------------------------
,yours Thomas Zangl - http://www.velocityreviews.com/forums/(E-Mail Removed) - http://www.tzis.net/ -
- Freelancer - IT Consulting & Software Development -
Use Y.A.M.C! now! Get it at http://www.borg-kindberg.ac.at/yamc/
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      01-11-2006


Thomas Zangl wrote:


> <invoice>


> <billitems>
>
> <item>
> <PName>Kiwi</PName>
> <PPrice>0.900</PPrice>
> <PId>1</PId>
> <BCQuant>15</BCQuant>
>
> </item>
>
> <item>
> <PName>Apfel</PName>
> <PPrice>0.500</PPrice>
> <PId>3</PId>
> <BCQuant>10</BCQuant>
>
> </item>
>
> </billitems>
> </invoice>
>
> Now I want to have the sum of
> /invoice/billitems/item/BCQuant*/invoice/billitems/item/PPrice


XPath 2.0 with a for..in expression has the expressive power to do that
easily and with one expression e.g.

<xsl:value-of select="sum(for $item in invoice/billitems/item return
$item/PPrice * $item/BCQuant)" />

If you want to do it with XSLT 1.0 and XPath 1.0 then you need to write
a recursive template where you pass in the item elements, compute the
BCQuant * PPPrice for the first item and pass that value and the
remaining items to the template until all items have been processed:

<xsl:template name="computeTotal">
<xslaram name="items" />
<xslaram name="currentTotal" select="0" />
<xsl:choose>
<xsl:when test="$items">
<xsl:call-template name="computeTotal">
<xsl:with-param name="items"
select="$items[position() &gt; 1]" />
<xsl:with-param name="currentTotal"
select="$items[1]/PPrice * $items[1]/BCQuant + $currentTotal" />
</xsl:call-template>
</xsl:when>
<xsltherwise>
<xsl:value-of select="$currentTotal" />
</xsltherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="/">
<html>
<head>
<title>sum</title>
</head>
<body>
<p>Sum is:
<xsl:call-template name="computeTotal">
<xsl:with-param name="items"
select="invoice/billitems/item" />
</xsl:call-template>
</p>
</body>
</html>
</xsl:template>

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
Andrew Schorr
Guest
Posts: n/a
 
      01-11-2006
Just for fun, here's how to do it using xgawk (from
http://sourceforge.net/projects/xmlgawk):

xgawk -lxml '
{
switch (XMLEVENT) {
case "STARTELEM":
PATH = PATH"/"XMLNAME
if (PATH == "/invoice/billitems/item")
delete chardata
break
case "CHARDATA":
if ($1 != "")
chardata[PATH] = $0
break
case "ENDELEM":
if (PATH == "/invoice/billitems/item")
sum += chardata["/invoice/billitems/item/BCQuant"]* \
chardata["/invoice/billitems/item/PPrice"]
PATH = substr(PATH,1,length(PATH)-length(XMLNAME)-1)
break
}
}

END {
printf "Sum = %.3f\n",sum
}'

 
Reply With Quote
 
Dimitre Novatchev
Guest
Posts: n/a
 
      01-12-2006
Google for FXSL

Cheers,
Dimitre Novatchev

"Thomas Zangl" <(E-Mail Removed)> wrote in message
news:dq15ra$gbk$(E-Mail Removed)-gems.net...
> Hi!
>
> I have an XML like this:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <invoice>
> <bill>
> <BId>20</BId>
> <CId>73</CId>
> <BDate>2006-01-10</BDate>
> <BStatus>0</BStatus>
> </bill>
>
> <billitems>
>
> <item>
> <PName>Kiwi</PName>
> <PPrice>0.900</PPrice>
> <PId>1</PId>
> <BCQuant>15</BCQuant>
>
> </item>
>
> <item>
> <PName>Apfel</PName>
> <PPrice>0.500</PPrice>
> <PId>3</PId>
> <BCQuant>10</BCQuant>
>
> </item>
>
> </billitems>
> </invoice>
>
> Now I want to have the sum of
> /invoice/billitems/item/BCQuant*/invoice/billitems/item/PPrice
>
> (=total price)
> Any ideas how to do?
>
> TIA!
> --
> ----------------------------------------------------------------
> ,yours Thomas Zangl - (E-Mail Removed) - http://www.tzis.net/ -
> - Freelancer - IT Consulting & Software Development -
> Use Y.A.M.C! now! Get it at http://www.borg-kindberg.ac.at/yamc/



 
Reply With Quote
 
William Park
Guest
Posts: n/a
 
      01-13-2006
Thomas Zangl <(E-Mail Removed)> wrote:
> Hi!
>
> I have an XML like this:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <invoice>
> <bill>
> <BId>20</BId>
> <CId>73</CId>
> <BDate>2006-01-10</BDate>
> <BStatus>0</BStatus>
> </bill>
>
> <billitems>
>
> <item>
> <PName>Kiwi</PName>
> <PPrice>0.900</PPrice>
> <PId>1</PId>
> <BCQuant>15</BCQuant>
>
> </item>
>
> <item>
> <PName>Apfel</PName>
> <PPrice>0.500</PPrice>
> <PId>3</PId>
> <BCQuant>10</BCQuant>
>
> </item>
>
> </billitems>
> </invoice>
>
> Now I want to have the sum of
> /invoice/billitems/item/BCQuant*/invoice/billitems/item/PPrice
>
> (=total price)
> Any ideas how to do?



You need XML parser and floating point calculator. Fortunately, Bash
shell can do both. (See my .sig)

start() # Usage: start tag att=value ...
{
case ${XML_TAG_STACK[0]} in
item) BCQuant=0 PPrice=0 ;;
billitems) rpn clear ;;
esac
}
data() # Usage: data text
{
case ${XML_TAG_STACK[*]|,.} in
BCQuant.item.billitems.invoice) BCQuant="$1" ;;
PPrice.item.billitems.invoice) PPrice="$1" ;;
esac
}
end() # Usage: end tag
{
case ${XML_TAG_STACK[0]} in
item) rpn $BCQuant $PPrice x + =x ;;
esac
}
expat -s start -d data -e end file.xml

The above code will give you running sum. 'rpn' is shell builtin
command, which is full RPN calculator (modelled after HP-42S).

--
William Park <(E-Mail Removed)>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VOIP over VPN over TCP over WAP over 3G Theo Markettos UK VOIP 2 02-14-2008 03:27 PM
Computed field in temp table causes error in .NET John Dalberg ASP .Net 1 02-03-2006 12:14 AM
How is Object.hashCode() computed? Aryeh.Friedman@gmail.com Java 2 05-24-2005 02:16 AM
Content-type: multipart/form-data - how is boundary computed Pascal Steiss HTML 1 03-19-2005 10:00 AM
Computed columns in datagrid Raymond Du ASP .Net 3 12-02-2004 05:32 AM



Advertisments