473,608 Members | 2,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange behaviour with identical XmlNode.SelectS ingleNode(xpath Query) 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.LoadXm l("<book><title >Bugs</title></book>");
XmlNode book1 = smallDoc.Select SingleNode(@"bo ok");
XmlNode title1 = book1.SelectSin gleNode(@"/book/title");
Assert.IsNotNul l(title1, "title1");

XmlDocument largeDoc = new XmlDocument();
largeDoc.LoadXm l("<books><book ><title>Bugs</title></book></books>");
XmlNode book2 = largeDoc.Select SingleNode(@"bo oks/book");
Assert.AreEqual (book1.OuterXml , book2.OuterXml) ;
XmlNode title2 = book2.SelectSin gleNode(@"/book/title");
Assert.IsNotNul l(title2, "title2"); // This test fails!!
}

Thanks,
Rune
Nov 12 '05 #1
4 4949
"Rune" <ru*****@yahoo. no> wrote in message news:f6******** *************** ***@posting.goo gle.com...
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.SelectSin gleNode(@"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.goo gle.com...
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.SelectSin gleNode(@"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
2695
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 switch on and off certain rules. One idea I had was to have two queries UNIONed together with a simple switch selecting which half was to operate (I know it sounds like there are probably better ways of doing this but I have my reasons). To cut...
4
2345
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 expected and match. I can query it in several ways as detailed below. The 5th version of the simple query below based on the second copy of the view fails, but works under the first copy. /*1 Statement below works*/ SELECT * FROM AgentHierarchy
4
3243
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 a new table just for testing called "blah" and it had the same results. My statements: "UPDATE GiftCerts SET Paid=1 WHERE CertSerial='2005-0001'" and "INSERT INTO blah (test) VALUES ('something')"
0
346
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 nunit test that exposes the problem. I have run the test under both the 1.0 and 1.1 framework with the same result. public void XPathBooks() { XmlDocument smallDoc = new XmlDocument();
4
3549
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 the same product id)there are likely to be more than one payment recorded. Nonetheless, when I run the queries individually and the query relates to a table that provides referential integrity through a one to many relationship, the query does what...
1
2090
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 queries to a SQL Server 2000 Standard Edition SP4 running on Windows 2000 Server with 2.5 GB of memory. About 11 of these queries are over views (all over the same table) and these queries are all done from JDBC but I am not sure that matters. ...
1
2821
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. Despite entering the required lines to "my.ini" (the new my.cnf) through notepad AND MySQL Administrator, the cache does not work. So, today I took a peek at the 'Health' tab in MySQL Administrator.
5
2276
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 the query produced and opened it from a MS Query it started giving strange results. The first query when run alone returns 22 records, some of which have identical values in all fields. This is 100% correct. The second query returns nothing....
2
8393
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 SelectSingleNode method with the XPath query Here is a test console application i wrote to better demonstrate my issue and.. help you help me :)
0
8000
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
8495
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
8470
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
8145
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
6815
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
5475
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
3960
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
4023
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1589
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.