473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1656
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.CreateNavigator();

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

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

XPathNavigator nav2 = doc2.CreateNavigator();

return nav2.Matches(expr);
-- 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****************@TK2MSFTNGP12.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****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP12.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.CreateNavigator();

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

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

XPathNavigator nav2 = doc2.CreateNavigator();

return nav2.Matches(expr);
-- 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****************@TK2MSFTNGP12.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 DummyXpathNavigator : XPathNavigator {
public override XPathNavigator Clone() {
return new DummyXpathNavigator();
}

public override XPathNodeType NodeType {
get { return XPathNodeType.Root; }
}

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(string localName, string
namespaceURI) {
return string.Empty;
}

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

public override bool MoveToFirstAttribute() {
return false;
}

public override bool MoveToNextAttribute() {
return false;
}

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

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

public override bool MoveToFirstNamespace(XPathNamespaceScope
namespaceScope) {
return false;
}

public override bool MoveToNextNamespace(XPathNamespaceScope
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 MoveToFirstChild() {
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 XPathNodeIterator SelectDescendants(string name,
string namespaceURI, bool matchSelf) {
return null;
}

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

And its usage:

XPathDocument doc = new XPathDocument(new StringReader("<foo/>"));
XPathNavigator nav = doc.CreateNavigator();
XPathExpression xe = XPathCompiler.Compile("/foo");
XPathNodeIterator ni = nav.Select(xe);
while (ni.MoveNext()) {
Console.WriteLine(ni.Current.Name);
}

--
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_PLEASEtkachenko.com> wrote in message
news:%2****************@TK2MSFTNGP10.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 DummyXpathNavigator : XPathNavigator {
public override XPathNavigator Clone() {
return new DummyXpathNavigator();
}

public override XPathNodeType NodeType {
get { return XPathNodeType.Root; }
}

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(string localName, string
namespaceURI) {
return string.Empty;
}

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

public override bool MoveToFirstAttribute() {
return false;
}

public override bool MoveToNextAttribute() {
return false;
}

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

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

public override bool MoveToFirstNamespace(XPathNamespaceScope
namespaceScope) {
return false;
}

public override bool MoveToNextNamespace(XPathNamespaceScope
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 MoveToFirstChild() {
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 XPathNodeIterator SelectDescendants(string name, string namespaceURI, bool matchSelf) {
return null;
}

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

And its usage:

XPathDocument doc = new XPathDocument(new StringReader("<foo/>"));
XPathNavigator nav = doc.CreateNavigator();
XPathExpression xe = XPathCompiler.Compile("/foo");
XPathNodeIterator ni = nav.Select(xe);
while (ni.MoveNext()) {
Console.WriteLine(ni.Current.Name);
}

--
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
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...
3
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...
4
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...
0
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...
8
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...
6
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 ...
18
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...
2
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...
3
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...

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.