Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   Sum over computed value ? (http://www.velocityreviews.com/forums/t170401-sum-over-computed-value.html)

Thomas Zangl 01-10-2006 08:35 PM

Sum over computed value ?
 
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 - thomas@tzis.net - 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/

Martin Honnen 01-11-2006 01:41 PM

Re: Sum over computed value ?
 


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">
<xsl:param name="items" />
<xsl:param 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>
<xsl:otherwise>
<xsl:value-of select="$currentTotal" />
</xsl:otherwise>
</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/

Andrew Schorr 01-11-2006 04:08 PM

Re: Sum over computed value ?
 
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
}'


Dimitre Novatchev 01-12-2006 08:42 AM

Re: Sum over computed value ?
 
Google for FXSL

Cheers,
Dimitre Novatchev

"Thomas Zangl" <usenet@tzis.net> wrote in message
news:dq15ra$gbk$1@news.soft-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 - thomas@tzis.net - 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/




William Park 01-13-2006 06:01 AM

Re: Sum over computed value ?
 
Thomas Zangl <usenet@tzis.net> 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 <opengeometry@yahoo.ca>, 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/


All times are GMT. The time now is 11:34 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.