How to use the contextNode in document.evaluate()?

Björn Langhof

Usenet Archive
Joined
Sep 29, 2006
Messages
10
Reaction score
0
Hello.

I want to evaluate a XPath-Expression only on a subtree of the whole xml-document.

1. I select a node of the XML-document
2. Then a want to select specific nodes below the node chosen in 1.

I thought the parameter contextNode in
var xpathResult = document.evaluate(xpathExpression, contextNode, namespaceResolver, resultType, result) would do this like described here http://www-xray.ast.cam.ac.uk/~jgraham/mozilla/xpath-tutorial.html

But this doesn't work. It still selects all nodes from the whole document and not only of the subtree.

At the bottom is a short example.

The XML-Document:
<gods>
<god name="Kibo">
<amount value="3"/>
</god>
<god name="Xibo">
<amount value="6"/>
</god>
<\/gods>

1. I select the node <god name="Kibo"> and name it godKibo.
2. I want to get the nodelist of all <amount>-Nodes below godKibo. var expr = '//amount'; var xpathResult = xmlDocument.evaluate( expr, godKibo, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null );

But this gives a list of all <amount>-Nodes of the document. How do I have to change expr to work?

I know if I set:

var expr = '//god[@name="Kibo"]/amount';
var xpathResult = xmlDocument.evaluate(
expr,
xmlDocument,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null
);

it will work. But I don't want to repeat the expression to find the godKibo-node.

Thanks for your help.

Greetings,
Björn

Here is the full example.

var xmlSource = '';
xmlSource += '<gods>';
xmlSource += '<god name="Kibo">';
xmlSource += '<amount value="3"/>';
xmlSource += '</god>';
xmlSource += '<god name="Xibo">';
xmlSource += '<amount value="6"/>';
xmlSource += '</god>';
xmlSource += '<\/gods>';
var xmlDocument = new DOMParser.parseFromString(xmlSource, 'text/xml');

var xmlSerializer = new XMLSerializer;

// Selecting the node god with the name Kibo

var godKibo = xmlDocument.evaluate(
'//god[@name="Kibo"]',
xmlDocument,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue;

// Selecting the node amount below the node godKibo

var expr = '//amount';

var xpathResult = xmlDocument.evaluate(
expr,
godKibo,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null
);

// This alert returns 2 although I thought it would
// search only below the godKibo node and return 1

alert(xpathResult.snapshotLength);
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
Björn Langhof said:
1. I select the node <god name="Kibo"> and name it godKibo.
2. I want to get the nodelist of all <amount>-Nodes below godKibo.
var expr = '//amount';

Use a relative XPath expression then, if you use
//amount
then you always search from the root node, if you simply used e.g amount then child elements of name 'amount' relative to the context nodes are searched. If you use e.g. .//amount then descendant elements of name 'amount' relative to the context nodes are searched.
 

Björn Langhof

Usenet Archive
Joined
Sep 29, 2006
Messages
10
Reaction score
0
Martin said:
Use a relative XPath expression then, if you use
//amount
then you always search from the root node, if you simply used e.g amount then child elements of name 'amount' relative to the context nodes are searched. If you use e.g. .//amount then descendant elements of name 'amount' relative to the context nodes are searched.

Works great. Thank you very much for the quick response.

Björn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,243
Members
248,133
Latest member
DujonT

Latest Threads

Top