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