1

I looking to add a total (Sum) on the bottom of a table column with numbers. I can do this by using the XSLT 1.0 option mentioned in Aggregate-function for sum and product in XPath

However, I could also parse the XML source first in Java (it's part of a TeamSite install, which uses Java & XSLT 1.0) and add the sum there. Does anybody know how the efficiency of both solutions compare?

Note: the Java code does not parse the XML source already, so there is no efficiency to be gained.

Update

I made both solutions, and the Java solution seems to be a bit faster. However: I don't have exact metrics and I don't have a big example data set. I'm going to use the Java solution, mainly because of re-usability (i.e. using sub totals and grand totals) and the caching options that are available in Java.

Community
  • 1
  • 1
DdW
  • 890
  • 1
  • 18
  • 30
  • Have you tried the two alternatives? What did you find? – Tomalak Nov 13 '13 at 14:05
  • This question was part of my orientation: if anyone would point out that one if way more efficient than the other, I wouldn't bother building the other... However, if there won't be an answer before this piece of work is due, I'll post my findings :) – DdW Nov 13 '13 at 15:11
  • 1
    That's always the case with performance questions. They are moot until someone measures it specifically. A better question would be *"I have tried these two ways. This way is by an order of magnitude faster, can anyone explain why?"*. Also: Don't optimize ahead of time. Make it work, and if it's slow, make it faster. :) – Tomalak Nov 13 '13 at 15:19
  • This isn't a science question: it is a question that came up during development, and thinking ahead is a good practice imho... an other one: not doing the work somebody else has done before: that was my intent – DdW Nov 13 '13 at 20:39
  • I realize that, but my point is that no one knows these things without measuring them. Especially when there is no implementation to look at (like in your case), all bets are off. It's highly unlikely that you will get a dependable performance prediction from anyone on the basis of a text description alone. Reading your update I think you have made a very reasonable decision. – Tomalak Nov 13 '13 at 21:20

1 Answers1

2

There are many ways of doing it in Java, and there are many ways of doing it in XSLT, both in terms of how you write your code, and in terms of what products you use (e.g. different parsers, different tree models, different XSLT processors). Your results may well tell you more about your Java and XSLT programming skills than about the technology you are using. As @Tomalak says, measure it, publish your results, and invite people to comment on their validity / reproducibility.

It's quite likely the cost will be dominated by the cost of XML parsing, which is in principle the same either way. However, some tree models are more efficient than others; the DOM, which most people use, is probably the worst choice.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164