By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,481 Members | 1,211 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,481 IT Pros & Developers. It's quick & easy.

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

P: n/a
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/~jgrah...-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);
Sep 29 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a


Björn Langhof wrote:
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 29 '06 #2

P: n/a
Martin Honnen schrieb:
Björn Langhof wrote:
>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.
Works great. Thank you very much for the quick response.

Björn
Sep 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.