Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XPath works in XML Spy, but not in javascript node.selectSingleNode

Status
Not open for further replies.

iporter

Programmer
Joined
Mar 24, 2004
Messages
2
Location
GB
Hi, thanks in advance for any help. I have an xml file and use javascript/XPath to pull data to an HTML page. In XML Spy, using XPath evaluator, the below XPath returns the appropriate value. However, in the browser, I get 'object required'.


First, I select a single level1 node based on it's id attribute, using

var node = nodes.selectSingleNode("//root/level1[@id=1]");

This works fine. Next I try to select a single node's value from within that node, using

innerNodeVal = node.selectSingleNode("//level1/level2/level3/level4[@id=1]/wanted node").text;

Any thoughts? Putting just 1 slash or 0 slashes at the beginning of the XPath string doesn't solve it, but all work in XML Spy.

Thanks again,
Iain
 
When you evaluate the x-path in Spy, it is taken from the root-node.
However, in the script function, you take it from the context of node. (being: //root/level1[@id=1] )
So: take it from level1.
innerNodeVal = node.selectSingleNode("level2/level3/level4[@id=1]/wanted node").text;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top