473,804 Members | 2,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Same query against multiple docs

Hello,

I have a collection of XML strings and want to find those that match a given
XPath expression.

I see lots of stuff to support evaluating the same expression against
different nodes in a single document but as far as I can see, if I want to
evaluate the expression against different docs, I have to recreate it
everytime.

Is that correct?

michael


Nov 11 '05 #1
5 1672
Compile the expression using the Compile method on the XPathNavigator and
cache the returned XPathExpression object. You can then use this object for
querying over differnt documents and you dont have to compile it every time.

XPathDocument doc1 = new XPathDocument(" doc1.xml");

XPathNavigator nav1 = doc1.CreateNavi gator();

XPathExpression expr = nav1.Compile("/");

XPathDocument doc2 = new XPathDocument(" dump.xml");

XPathNavigator nav2 = doc2.CreateNavi gator();

return nav2.Matches(ex pr);
-- Asad
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/copyright.htm.

"mono" <mikeg@n_o_s_p_ a_mcimage.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Hello,

I have a collection of XML strings and want to find those that match a given XPath expression.

I see lots of stuff to support evaluating the same expression against
different nodes in a single document but as far as I can see, if I want to evaluate the expression against different docs, I have to recreate it
everytime.

Is that correct?

michael

Nov 11 '05 #2
Asad,

Thanks, that answers the question.

I thought the XPathExpression would work only for the XPathNavigator that
created it.

Why isn't there a way of creating the expression without involving a
specific document, as can be done with Regex (which I see as analoguous)?

michael

"SQL Server Development Team [MSFT]" <sq****@microso ft.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Compile the expression using the Compile method on the XPathNavigator and
cache the returned XPathExpression object. You can then use this object for querying over differnt documents and you dont have to compile it every time.
XPathDocument doc1 = new XPathDocument(" doc1.xml");

XPathNavigator nav1 = doc1.CreateNavi gator();

XPathExpression expr = nav1.Compile("/");

XPathDocument doc2 = new XPathDocument(" dump.xml");

XPathNavigator nav2 = doc2.CreateNavi gator();

return nav2.Matches(ex pr);
-- Asad
This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/copyright.htm.

"mono" <mikeg@n_o_s_p_ a_mcimage.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Hello,

I have a collection of XML strings and want to find those that match a

given
XPath expression.

I see lots of stuff to support evaluating the same expression against
different nodes in a single document but as far as I can see, if I want

to
evaluate the expression against different docs, I have to recreate it
everytime.

Is that correct?

michael


Nov 11 '05 #3
Here is dummy XPathNavigator implementation I was talking about. Its main
advantage is almost zero memory footprint - it's just an access to parent's
Compile() method implementation.
I wonder if I overlooked any simpler solution (except for no reflection
hacks)? If not I hope V2 API has solved that problem already.

//Safe wrapper to hide dummy XPathNavigator
public class XPathCompiler {
private class DummyXpathNavig ator : XPathNavigator {
public override XPathNavigator Clone() {
return new DummyXpathNavig ator();
}

public override XPathNodeType NodeType {
get { return XPathNodeType.R oot; }
}

public override string LocalName {
get { return String.Empty; }
}

public override string NamespaceURI {
get { return String.Empty; }
}

public override string Name {
get { return String.Empty; }
}

public override string Prefix {
get { return String.Empty; }
}

public override string Value {
get { return String.Empty; }
}

public override string BaseURI {
get { return String.Empty; }
}

public override String XmlLang {
get { return String.Empty; }
}

public override bool IsEmptyElement {
get { return false; }
}

public override XmlNameTable NameTable {
get { return null; }
}

public override bool HasAttributes {
get { return false; }
}

public override string GetAttribute(st ring localName, string
namespaceURI) {
return string.Empty;
}

public override bool MoveToAttribute (string localName, string
namespaceURI) {
return false;
}

public override bool MoveToFirstAttr ibute() {
return false;
}

public override bool MoveToNextAttri bute() {
return false;
}

public override string GetNamespace(st ring name) {
return string.Empty;
}

public override bool MoveToNamespace (string name) {
return false;
}

public override bool MoveToFirstName space(XPathName spaceScope
namespaceScope) {
return false;
}

public override bool MoveToNextNames pace(XPathNames paceScope
namespaceScope) {
return false;
}

public override bool HasChildren {
get { return false; }
}

public override bool MoveToNext() {
return false;
}

public override bool MoveToPrevious( ) {
return false;
}

public override bool MoveToFirst() {
return false;
}

public override bool MoveToFirstChil d() {
return false;
}

public override bool MoveToParent() {
return false;
}

public override void MoveToRoot() {}

public override bool MoveTo( XPathNavigator other ) {
return false;
}

public override bool MoveToId(string id) {
return false;
}

public override bool IsSamePosition( XPathNavigator other) {

return false;
}

public override XPathNodeIterat or SelectDescendan ts(string name,
string namespaceURI, bool matchSelf) {
return null;
}

public override XPathNodeIterat or SelectChildren( string name,
string namespaceURI) {
return null;
}
public override XPathNodeIterat or SelectChildren( XPathNodeType
nodeType) {
return null;
}
public override XmlNodeOrder ComparePosition ( XPathNavigator
navigator ) {
return new XmlNodeOrder();
}
}
//Singleton
private static XPathNavigator _nav = new DummyXpathNavig ator();
//Here we go
public static XPathExpression Compile(string xpath) {
return _nav.Compile(xp ath);
}
}

And its usage:

XPathDocument doc = new XPathDocument(n ew StringReader("< foo/>"));
XPathNavigator nav = doc.CreateNavig ator();
XPathExpression xe = XPathCompiler.C ompile("/foo");
XPathNodeIterat or ni = nav.Select(xe);
while (ni.MoveNext()) {
Console.WriteLi ne(ni.Current.N ame);
}

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #4
Oleg,

Thanks for that.

A minor downside IMHO is that is introduces another class which other
developers are going to need to understand. Asad solution doesn't have this
drawback.

michael

"Oleg Tkachenko" <oleg@NO_SPAM_P LEASEtkachenko. com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Here is dummy XPathNavigator implementation I was talking about. Its main
advantage is almost zero memory footprint - it's just an access to parent's Compile() method implementation.
I wonder if I overlooked any simpler solution (except for no reflection
hacks)? If not I hope V2 API has solved that problem already.

//Safe wrapper to hide dummy XPathNavigator
public class XPathCompiler {
private class DummyXpathNavig ator : XPathNavigator {
public override XPathNavigator Clone() {
return new DummyXpathNavig ator();
}

public override XPathNodeType NodeType {
get { return XPathNodeType.R oot; }
}

public override string LocalName {
get { return String.Empty; }
}

public override string NamespaceURI {
get { return String.Empty; }
}

public override string Name {
get { return String.Empty; }
}

public override string Prefix {
get { return String.Empty; }
}

public override string Value {
get { return String.Empty; }
}

public override string BaseURI {
get { return String.Empty; }
}

public override String XmlLang {
get { return String.Empty; }
}

public override bool IsEmptyElement {
get { return false; }
}

public override XmlNameTable NameTable {
get { return null; }
}

public override bool HasAttributes {
get { return false; }
}

public override string GetAttribute(st ring localName, string
namespaceURI) {
return string.Empty;
}

public override bool MoveToAttribute (string localName, string
namespaceURI) {
return false;
}

public override bool MoveToFirstAttr ibute() {
return false;
}

public override bool MoveToNextAttri bute() {
return false;
}

public override string GetNamespace(st ring name) {
return string.Empty;
}

public override bool MoveToNamespace (string name) {
return false;
}

public override bool MoveToFirstName space(XPathName spaceScope
namespaceScope) {
return false;
}

public override bool MoveToNextNames pace(XPathNames paceScope
namespaceScope) {
return false;
}

public override bool HasChildren {
get { return false; }
}

public override bool MoveToNext() {
return false;
}

public override bool MoveToPrevious( ) {
return false;
}

public override bool MoveToFirst() {
return false;
}

public override bool MoveToFirstChil d() {
return false;
}

public override bool MoveToParent() {
return false;
}

public override void MoveToRoot() {}

public override bool MoveTo( XPathNavigator other ) {
return false;
}

public override bool MoveToId(string id) {
return false;
}

public override bool IsSamePosition( XPathNavigator other) {

return false;
}

public override XPathNodeIterat or SelectDescendan ts(string name, string namespaceURI, bool matchSelf) {
return null;
}

public override XPathNodeIterat or SelectChildren( string name,
string namespaceURI) {
return null;
}
public override XPathNodeIterat or SelectChildren( XPathNodeType nodeType) {
return null;
}
public override XmlNodeOrder ComparePosition ( XPathNavigator
navigator ) {
return new XmlNodeOrder();
}
}
//Singleton
private static XPathNavigator _nav = new DummyXpathNavig ator();
//Here we go
public static XPathExpression Compile(string xpath) {
return _nav.Compile(xp ath);
}
}

And its usage:

XPathDocument doc = new XPathDocument(n ew StringReader("< foo/>"));
XPathNavigator nav = doc.CreateNavig ator();
XPathExpression xe = XPathCompiler.C ompile("/foo");
XPathNodeIterat or ni = nav.Select(xe);
while (ni.MoveNext()) {
Console.WriteLi ne(ni.Current.N ame);
}

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #5
mono wrote:
A minor downside IMHO is that is introduces another class which other
developers are going to need to understand. Well, actually I don't see any trouble a developer can have with a
utility class with single static Compile() method, which exactly
duplicates syntax and semantics of XPathNavigator' s Compile() method.
Asad solution doesn't have this
drawback.

Yeah, but it requires live XmlDocument or XpathDocument object to
compile an expression. It's ok for lazy pattern, where first time the
expression is needed it's created, but eager initialization becomes
unfeasible without sort of dummy XmlDocument or XpathDocument objects.
So it's usual learning vs performance tradeoff.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #6

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

Similar topics

3
7564
by: Benjamin Dickgießer | last post by:
Hi, I want to create a sql query but don't know if this is possible with mysql. The Query should do the following: Select all db entries from table in which entry a is smaller than the number (count) of all db entries with criteria x from another table. Thx for your help! Benjamin Dickgießer
3
2454
by: JT | last post by:
I have a MySQL fulltext search form in place and I now want to filter the results further. I've added a few multiple select boxes on my form (ResourceType and Topic), and I'm able to build and execute the SQL queries properly. The problem is that when I add the criteria for "ResourceTyoe" and "Topic", it seems to take precedence over the fulltext matching. In The two query examples below I'm searching on a single keyword "safety"....
4
6778
by: Bob Hotschins | last post by:
I've joined several columns from several tables, and I would like to perform a relevance match against these multiple columns. It looks something like this: SELECT * FROM table1 LEFT JOIN column1 ON table2.column1 LEFT JOIN column 2 ON table3.column1 WHERE MATCH ( table2.column1, table3.column1)
0
2014
by: unixman | last post by:
As usual, it is 2:00am, and I'm pulling my hair out, finally resorting to posting in the newsgroups for help. :) Simple problem, in theory. Given table "map": CREATE TABLE map ( entry_id int(10) unsigned NOT NULL auto_increment, piece_id int(10) unsigned NOT NULL default '0',
8
3378
by: Rigga | last post by:
Hi, I am new to mysql and need help on how to join tables. I have a database which contains 4 tables, the main table contains information by date order and the other 3 contain data also in date order. So I need to write a query that retrieves all the information for one record, lets say I want to query on the main table any entry that is for the 2004-01-06 and this date is also in a field called 'Date' in the other tables, how do I go...
6
4852
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID SalesManName AT Alan Time
18
3399
by: Gleep | last post by:
I've searched google intensely on this topic and it seems noone really knows how to approch this. The goal I don't want clients to give out their usernames and passwords to friends, since the site relies on subscrption fees. Sessions ID's are matched between the browser and the server. So a users can login with same username and password and those sessions are tracked individually. Some suggest create table fields with the session ID...
2
2825
by: yxktmp | last post by:
Hi; I have a main starting query for most of my reporting. I branch out from this query to other queries. This main query contains multiple form control filters (query criterias) . I want to use this query from multiple forms because each report needs different filters on this query. Is there a way to to this like having multiple copies of this query keeping the name same, like an alias name? I would appreciate ideas on this. Thanks! ...
3
2926
by: evenrik | last post by:
An a redhat box I have root, apache and other normal users run code that uses the logging module to write to the same log file. Since umasks are set to 2 or 022 this gets permission errors. I have fixed my issue by patching the logging code everywhere there is an open for write with: try: old_umask = os.umask(0) # open for write here finally:
0
9714
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
9594
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,...
1
10347
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
10090
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
9173
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5531
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...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3832
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.