473,382 Members | 1,349 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,382 software developers and data experts.

Strange behaviour with identical XmlNode.SelectSingleNode(xpathQuery) queries

I have two queries that appear to be exactly the same, but one of them
returns null while the other one returns a valid result!
Can anyone provide an explanation to why this is so? Below is an nunit
test that exposes the problem. I have run the test under both the 1.0
and 1.1 framework with the same result.

[Test]
public void XPathBooks()
{
XmlDocument smallDoc = new XmlDocument();
smallDoc.LoadXml("<book><title>Bugs</title></book>");
XmlNode book1 = smallDoc.SelectSingleNode(@"book");
XmlNode title1 = book1.SelectSingleNode(@"/book/title");
Assert.IsNotNull(title1, "title1");

XmlDocument largeDoc = new XmlDocument();
largeDoc.LoadXml("<books><book><title>Bugs</title></book></books>");
XmlNode book2 = largeDoc.SelectSingleNode(@"books/book");
Assert.AreEqual(book1.OuterXml, book2.OuterXml);
XmlNode title2 = book2.SelectSingleNode(@"/book/title");
Assert.IsNotNull(title2, "title2"); // This test fails!!
}

Thanks,
Rune
Nov 12 '05 #1
4 4925
"Rune" <ru*****@yahoo.no> wrote in message news:f6**************************@posting.google.c om...
I have two queries that appear to be exactly the same, but one of them
returns null while the other one returns a valid result!
Can anyone provide an explanation to why this is so?


Rune,

The queries are the same, but the XML node tree you're querying
and the context in which you're querying it has changed.

The first query is "/book/title" against the following XML document,

<book>
<title> Bugs </title>
</book>

Where the context node for the XPath expression is <book>
(observe that <book>'s ParentNode is "#document"). The
XPath expression is absolute, so it starts at #document,
finds #document has child named <book>, and that <book>
has child named <title>.

The second query is "/book/title" against the following XML
document,

<books>
<book>
<title> Bugs </title>
</book>
</books>

The context node for the XPath expression is <book> (observe
that <book>'s ParentNode is "books" and not "#document"). The
XPath expression is absolute, so it begins at #document. It finds
that #document has no immediate child named "book" (it has one
named "books," but that doesn't match), therefore the query
returns null.

The XPath expression equivalent to the first expression when
the context node is <book> in the second document would be:

XmlNode title2 = book2.SelectSingleNode(@"title");

As a general practice when writing XPath expressions, try not
to spell out the context node explicitly. For instance, instead
of using the relative expression "../book/title" (which may not
produce a single node if <book> has siblings), just say "title".
Derek Harmon
Nov 12 '05 #2
"Rune" <ru*****@yahoo.no> wrote in message news:f6**************************@posting.google.c om...
I have two queries that appear to be exactly the same, but one of them
returns null while the other one returns a valid result!
Can anyone provide an explanation to why this is so?


Rune,

The queries are the same, but the XML node tree you're querying
and the context in which you're querying it has changed.

The first query is "/book/title" against the following XML document,

<book>
<title> Bugs </title>
</book>

Where the context node for the XPath expression is <book>
(observe that <book>'s ParentNode is "#document"). The
XPath expression is absolute, so it starts at #document,
finds #document has child named <book>, and that <book>
has child named <title>.

The second query is "/book/title" against the following XML
document,

<books>
<book>
<title> Bugs </title>
</book>
</books>

The context node for the XPath expression is <book> (observe
that <book>'s ParentNode is "books" and not "#document"). The
XPath expression is absolute, so it begins at #document. It finds
that #document has no immediate child named "book" (it has one
named "books," but that doesn't match), therefore the query
returns null.

The XPath expression equivalent to the first expression when
the context node is <book> in the second document would be:

XmlNode title2 = book2.SelectSingleNode(@"title");

As a general practice when writing XPath expressions, try not
to spell out the context node explicitly. For instance, instead
of using the relative expression "../book/title" (which may not
produce a single node if <book> has siblings), just say "title".
Derek Harmon
Nov 12 '05 #3
Of course :) Thanks a lot
Nov 12 '05 #4
Of course :) Thanks a lot
Nov 12 '05 #5

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

Similar topics

0
by: Ethel Aardvark | last post by:
I am running a 9.0.1 database on a W2K server and have come across some strange behaviour with a SQL query. I have a query which runs in a PL/SQL cursor which has several PL/SQL variables used to...
4
by: Ryan | last post by:
Bit of an obscure one here, so please bear with me. I have two copies of a database which should be identical. Both have a complex view which is identical. I can open the views and the data is as...
4
by: Paul | last post by:
I have run into a strange problem with a site I am working on. My SELECT queries work fine, but I just tried to add an UPDATE statement and it fails. A test showed that INSERT fails also. I created...
0
by: Rune | last post by:
I have two queries that appear to be exactly the same, but one of them returns null while the other one returns a valid result! Can anyone provide an explanation to why this is so? Below is an...
4
by: John Smith | last post by:
Isn't life a bitch! You know what you want but you don't know how to get it. I have produced 12 queries that calculate a payment profile over 12 months. For a number of the records (ie with...
1
by: loosecannon_1 | last post by:
Hello everyone, I am hoping someone can help me with this problem. I will say up front that I am not a SQL Server DBA, I am a developer. I have an application that sends about 25 simultaneous...
1
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work....
5
by: BillCo | last post by:
I'm having a problem with a union query, two simple queries joined with a union statement. It's created in code based on parameters. Users were noticing some inconsistant data and when I analysed...
2
by: Claudio | last post by:
Hi I'm trying to use XPath queries with streaming XML, but I cannot make it working. The solution I'm trying to implement is: create a XmlNode as soon as I have a full XML element and use the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.