Determine whether DOM node is matched by XPath expression

S

Steve Jorgensen

Working with the DOM (specifically, the MSXML DOM), I'm wondering if there's
an efficient way to check whether it would be matched by a given XPath
expression.

I've made it work to just run the XPath select on the document, looping
through all the nodes returned, and seeing if one of those is the same as the
node in question. If I need to check many expressions, though, this would not
be efficient.
 
M

Martin Honnen

Steve said:
Working with the DOM (specifically, the MSXML DOM), I'm wondering if there's
an efficient way to check whether it would be matched by a given XPath
expression.

If you are using script then the result of the selectNodes method has a
method matches to which you can pass a node so
I've made it work to just run the XPath select on the document, looping
through all the nodes returned, and seeing if one of those is the same as the
node in question.

you don't have to loop yourself. But with one particular XPath
expression you have to call selectNodes once to be able to check against
nodes you have.
If you don't use MSXML with script then you might need to cast the
result of selectNodes as needed.

Here is an example with JScript supposed to be run in the browser:

var xmlMarkup = [
'<gods>',
' <god power="42">Kibo</god>',
' <god power="-42">Xibo</god>',
' <god power="41">Maho</god>',
'</gods>'
].join('\r\n');
var xmlDocument = new ActiveXObject('Msxml2.DOMDocument.3.0');
xmlDocument.loadXML(xmlMarkup);
xmlDocument.setProperty('SelectionLanguage', 'XPath');

var selection = xmlDocument.selectNodes('/gods/god[@power > 30]');

var god = xmlDocument.selectSingleNode('/gods/god[. = "Kibo"]');

var matchNode = selection.matches(god);
if (matchNode == null) {
alert('no match');
}
else {
alert('match for context node with nodeType: ' + matchNode.nodeType +
'; nodeName: ' + matchNode.nodeName);
}


Docs are here:
<http://msdn.microsoft.com/library/d...html/b7238cd5-f64a-4b13-b394-cf90f7eea0df.asp>
 
S

Steve Jorgensen

If you are using script then the result of the selectNodes method has a
method matches to which you can pass a node so


you don't have to loop yourself. But with one particular XPath
expression you have to call selectNodes once to be able to check against
nodes you have.
If you don't use MSXML with script then you might need to cast the
result of selectNodes as needed.

....

Thanks - it looks like that'll help. Presumably, it might even be efficient
depending on whether the DOM uses lazy evaluation to retrieve matched nodes,
and knows how to do "matches" without building a list of matching nodes first
to do it.
 

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
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top