Determine whether DOM node is matched by XPath expression

Discussion in 'XML' started by Steve Jorgensen, Aug 14, 2005.

  1. 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.
    Steve Jorgensen, Aug 14, 2005
    #1
    1. Advertising

  2. 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.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/default.asp?url=/library/en-us/xmlsdk/html/b7238cd5-f64a-4b13-b394-cf90f7eea0df.asp>



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Aug 14, 2005
    #2
    1. Advertising

  3. On Sun, 14 Aug 2005 20:37:25 +0200, Martin Honnen <> wrote:

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


    ....

    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.
    Steve Jorgensen, Aug 14, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,442
  2. Replies:
    0
    Views:
    369
  3. Alastair Cameron
    Replies:
    1
    Views:
    7,391
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  4. Tjerk Wolterink
    Replies:
    2
    Views:
    1,418
    Dimitre Novatchev
    Aug 24, 2006
  5. DL
    Replies:
    0
    Views:
    90
Loading...

Share This Page