473,770 Members | 5,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determine whether DOM node is matched by XPath expression

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.
Aug 14 '05 #1
2 1515


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/
Aug 14 '05 #2
On Sun, 14 Aug 2005 20:37:25 +0200, Martin Honnen <ma*******@yaho o.de> 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.
Aug 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3488
by: ricky | last post by:
I want to return with only one xpath expression the first following node relative to a node with a XPTO attribute. Example: <node> xxx </node> <node XPTO="true"> yyy
4
4153
by: Jim Garrison | last post by:
I know how to use the name() function to access the name of the current node. How do I get the 'fully qualified' name, consisting of the path from the root to the current node? I.e. <a> <b> <c> </c> </b>
3
4547
by: Kathy Burke | last post by:
Hi again, I'm using the following xpath (works in visualizer) with a SelectSingleNode("xpath") statement. //Station/(WI])]/@order Problem is I get an error "expression passed to this method should result in a NodeSet". Of course, that (sort of) makes sense to me now (I suppose just an attribute couldn't be a nodeset, but how would I go
1
10959
by: Rob | last post by:
Hi, I am moving through an XML document using an XPath Navigator, and I'd like to be able to get the xpath expression for the location of the current node from the root node. Any ideas how to do this? I was hoping for a property of the navigator object, but I can't find it..... Thanks in advance for any help!
1
1486
by: Jan Limpens | last post by:
Hello, I am creating a simple CRUD application which uses xml files as data storage. I extended Textbox with a XPath expression and a XmlDocument property, this way I can easily read and update existing nodes' content. I was very happy with the solution until the first time I needed to insert a hierarchy of nodes. Imagine the following situation: <Entry> <Version xml:lang="en">
8
2198
by: Jean-François Michaud | last post by:
Who in the name of #%@! thought this one out?? I noticed this behavior when trying to debug a problem I was having. I used this logical expression and some XPATH in a specific sequence of instructions that allow me to transform a CALS table model into our own specific table model and I used this expression: <xsl:if test="self::node()=../CELL and self::node()">...
4
6497
by: MA | last post by:
Hi, How to access the total number of child nodes from a parent node. For example, I would like to get the total number of child nodes from <parent1and <parent2node. The SelectNodes method return the total number of <foldernodes (9) regardless of calling from the <parent1> and <parent2node. XML: ----------------------------------
5
50582
by: jorgedelgadolopez | last post by:
Hi all, I am using the xpathnavigator evaluate function on .net (xpath 1 right?). Now I need to expand the code to do multiple contains, compare dates (such as 'before', 'between' and 'after'), and so on. This is the thought: //person
7
2812
by: Tim Hallwyl | last post by:
Hi, there! As I understand the XPaht recommendation, the context node is a node; not a node-list, not XPath object -- but a single node. Now, the WS-BPEL 2.0 specification allows an XML simple type value to be the context of an XPath expression, by converting it to an XPath object -- either boolean, string or float. I do not see how this is possible, but I would like your thoughts on it.
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10228
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10057
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10002
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9869
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3575
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.