Re: How to perform calculation?

Subject: Re: How to perform calculation?
From: Brandon Ibach <bibach@xxxxxxxxxxxxxx>
Date: Tue, 16 May 2000 13:30:01 -0500
Quoting Philippe CASIDY <pcasidy@xxxxxxxxxxx>:
> I am wondering how I could do some calculations on values inside my
> documents. I am using JADE and TeX backend.
> <Test>
> <LINE>
> <DESC>Fisrt Value
> </LINE>
> <LINE>
> <DESC>Second Value
> </LINE>
> </Test>
> In my DSSSL, I would like to sum the values of QUANTITY and to have this
> result inserted in the output.
   While processing the <details> node:
	  (select-elements (descendants (current-node)) "QUANTITY")
	  (lambda (o n) (+ o (or (string->number (data n)) 0))) 0)

   If your version of [Open]Jade doesn't have (node-list-reduce) built
in, include this definition (lifted from the DSSSL standard):
	(define (node-list-reduce nl combine init)
	  (if (node-list-empty? nl) init
	      (node-list-reduce (node-list-rest nl) combine
	                        (combine init (node-list-first nl)))))

   In case you're not familiar with (node-list-reduce), here's what's
happening.  First, we get a node-list, starting with the descendants
of the current-node, which would be everything inside of <details>.
We filter that down to just the <quantity> elements, and pass the list
into (node-list-reduce), which applies its second argument, a
procedure, to its third argument (in this case, 0) and the first node
in our list.  The result becomes the new value for the third argument
and the procedure is repeated with the next node in the list until
there are no more nodes.
   The procedure we give to (node-list-reduce) simply extracts the
contents (data n) of the node, converts it to a number, substituting 0
if the conversion fails (returning #f), and adds it to the running
total, returning the result.
   I didn't actually test this, so if it doesn't work, let me know and
I'll fix it. :)

-Brandon :)

 DSSSList info and archive:

Current Thread