• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Basic XPath Sum question

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

 
Marshal
Posts: 25798
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's see what you tried.
 
Christopher Shrout
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 25798
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 25798
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

 
Ranch Hand
Posts: 729
7
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like this.

ps But I do not endorse any mis-use of document.write().>
 
Christopher Shrout
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 729
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Marshal
Posts: 25798
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 67446
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jQuery used to support XPath but no longer does.

I've had no problem using normal jQuery selectors when dealing with an XML DOM.
 
Christopher Shrout
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Marshal
Posts: 67446
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jQuery selectors are designed to identify element in a DOM; it could be an HTML DOM or an XML DOM.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic