Steve Jorgensen wrote:
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.DOMDocum ent.3.0');
xmlDocument.loa dXML(xmlMarkup) ;
xmlDocument.set Property('Selec tionLanguage', 'XPath');
var selection = xmlDocument.sel ectNodes('/gods/god[@power > 30]');
var god = xmlDocument.sel ectSingleNode('/gods/god[. = "Kibo"]');
var matchNode = selection.match es(god);
if (matchNode == null) {
alert('no match');
}
else {
alert('match for context node with nodeType: ' + matchNode.nodeT ype +
'; nodeName: ' + matchNode.nodeN ame);
}
Docs are here:
<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/xmlsdk/html/b7238cd5-f64a-4b13-b394-cf90f7eea0df.as p>
--
Martin Honnen
http://JavaScript.FAQTs.com/