Wednesday, March 19, 2008

A node is *not* a document...

Tonight I spent a considerable amount of time looking for a problem that wasn't there. In the end I was feeling kind of foolish...
I have a XMLDocument (in a XMLDomDocument from MSXML) that has the folowing structure (this is fake...):
<root><parent><child /></parent></root>
Now, I select a single node, using the xpath query:
/root/parent
and pass it to a procedure. In that procedure I was looking for the child. Suppose this is the procedure:
procedure foo(myNode: IXMLDomNode);
I tried to collect the child node using the xpath query:
/parent/child
and applied that to the passed node. After all, when I inspected myNode, the debugger told me that its structure was:
<parent><child /></parent>
So, why was I getting nil as a result of the xpath query?
The answer (which I found after two hours of debugging...) of course is simple. Even if you apply the xpath to a node, using '/' as the first character starts the query from the start of the document of the passed node. Not at the start of the node...

Boy oh boy, how foolish I was. The correct xpath to select the child was:

(can you guess....., read on .......)

child


Just a little moment of humiliation is called for.... To bed, to bed, my friend.

Bye,
Bart

No comments: