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

B

Björn Langhof

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);
 
M

Martin Honnen

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.
 
B

Björn Langhof

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

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top