aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Basic XPath Sum question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Basic XPath Sum question" Watch "Basic XPath Sum question" New topic
Author

Basic XPath Sum question

Christopher Shrout
Greenhorn

Joined: Jul 23, 2012
Posts: 8
Howdy all! Posting my question here as well since this seems to be an awesome community!

Using the following example XML file: W3Schools Books.xml I am trying to create an XPath string that will provide:

1) A sum of all "Price" fields in the document

2) An "Average" value of all the prices

There is sample code already present in their "tryit" section which I am trying to adapt: W3Schools example code
I've adapted their original path to display prices, but I cannot seem to get the "Sum" operator to work... Any help would be greately appreciated!

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

Let's see what you tried.
Christopher Shrout
Greenhorn

Joined: Jul 23, 2012
Posts: 8
I'll blurt my most recent code attempt onto here: (Go about halfway down for the line I added)



I *believe* that simply running "sum(/price)" should return the results that I'm looking for... but I believe I am putting it into the wrong spot... That code is returning the following error in IE:

Message: 'XPathResult' is undefined


If I can fix my syntax issue then I'm sure I can play around with the other operators like Average etc. Apparently the "selectNodes" command does not return "number" values, only strings. Hence the reason why I am *attempting* to beat a different code snippet into submission.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

So if I understand the sequence of events correctly, when you used XPathResult.ANY_TYPE then your Javascript code ran correctly and produced the output you expected, but when you used XPathResult.NUMBER_TYPE then you got a message saying "'XPathResult' is undefined"?

Christopher Shrout
Greenhorn

Joined: Jul 23, 2012
Posts: 8
Paul Clapham wrote:So if I understand the sequence of events correctly, when you used XPathResult.ANY_TYPE then your Javascript code ran correctly and produced the output you expected, but when you used XPathResult.NUMBER_TYPE then you got a message saying "'XPathResult' is undefined"?



It was set to "ANY_TYPE" initially and I changed it to "NUMBER_TYPE" but the result was the same - still "XPathResult is undefined". Sorry that I'm horribly green on this, just picked it up last week. Perhaps I need to iterate and add each individual value to create the sum. I've tried creating additional variables and throwing them in the for loop but it didn't help much... I'll hack something together real quick.
Christopher Shrout
Greenhorn

Joined: Jul 23, 2012
Posts: 8
Ok here is some "revised" code. It appears to be concatenating strings instead of actually performing any math: (Lines 32-37)



I get this result:
Sum: 030.0029.9949.9939.95


Plain english: Sum: 030.00, 29.99, 49.99, 39.95. Clearly it is concatenating and not performing any type of arithmetic. Something to do with the "selectNodes()" command...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

Well, yeah, that's what happens when you use the + operator on two Javascript strings. Let me move this post to the Javascript forum, since that's the way it seems to be going.
Christopher Shrout
Greenhorn

Joined: Jul 23, 2012
Posts: 8
Very true... the "parseDouble()" command doesn't seem to be working... I get an "Object Expected" error at line 33... There *must* be a way to sum these data in these nodes and then average them...

g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 464
    
    2
Like this.

ps But I do not endorse any mis-use of document.write().>
Christopher Shrout
Greenhorn

Joined: Jul 23, 2012
Posts: 8
g tsuji wrote:Like this.

ps But I do not endorse any mis-use of document.write().>


Thank you so much!!! I was totally missing the fact that a) I needed "parseFloat" and b) I needed to parse both the node value *and* the variable value each time. Amazing.

It seems that while the Firefox code has (sum(//price)) the I.E. code does not... is that a difference between the browsers? Are there certain XPath expressions that will not work with I.E.? Thank you again, huge relief.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 464
    
    2
It seems that while the Firefox code has (sum(//price)) the I.E. code does not... is that a difference between the browsers? Are there certain XPath expressions that will not work with I.E.?

You can write parallel code for them as long as the return is a node or a node-set and in that case the scalar value of sum is relegated to the application level (like what was shown in ie case above). But if the xpath expression is intended to return a scalar value there is indeed a difference. In firefox that is now a built-in support for that kind of return. For ie, you can build a stylesheet of output type='text' on the fly and call for the transformNode(). That has the appearence of more work indeed. (.Net can do the same, but that's another story.) During long period of time (in IT-scale), ie with msxml2 technology is long long way ahead of firefox. But people is having short memory and a large number of non-ie users are fanatic... Don't flame me, I won't response to the kind; and I do not dislike firefox neither, the contrary.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

I have the feeling that very few people are using XPath in the browser. I didn't really know it was possible, actually I never even thought about it, and this is the first question I have ever seen in the XML forum about it (and I've been moderating it for several years).

There's been a lot of work recently in writing code (e.g. jQuery) to abstract away browser-specific things in Javascript, with considerable success I think. But nothing for XPath, apparently. You could search the web for something like that but my guess is that you're unlikely to find any XPath abstraction code. So it's probably going to be up to you to test in all of the browsers you're interested in.

Edit: unless perhaps jQuery itself has XPath features? You could check that out.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

jQuery used to support XPath but no longer does.

I've had no problem using normal jQuery selectors when dealing with an XML DOM.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Christopher Shrout
Greenhorn

Joined: Jul 23, 2012
Posts: 8
Bear Bibeault wrote:jQuery used to support XPath but no longer does.

I've had no problem using normal jQuery selectors when dealing with an XML DOM.


As I'm quite green, how could I accomplish basically the same thing (creating a sum value) by using Jquery? I've looked through the w3schools Jquery tutorials, but it almost appears as though it was designed to manipulate items on the page rather than glean data.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

jQuery selectors are designed to identify element in a DOM; it could be an HTML DOM or an XML DOM.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Basic XPath Sum question
 
Similar Threads
Ajax Suggest
html basics
XSD - restrctions/enumeration - Only Weekdays
Basic of SOAP ?
problem with my xpath in selenium. getting a invalid xpath error