Subject: XPath Problem
Posted By: babloo81 Post Date: 11/13/2003 8:03:32 AM
How do I trap an error that can error due to Invalid Path specified in XPath. For example I am trying to look for attribute with some name say 'Test', which is not available.

Reply By: pgtips Reply Date: 11/13/2003 8:39:40 AM
are you talking about using selectSingleNode method in MSXML?  You could do:
Set node = domDoc.selectSingleNode("<<your XPATH>>")
If node Is Nothing Then
   ' its not there
Else
   ' it is there

Or you could do:
If domDoc.selectNodes("<<your XPATH>>").length = 0 Then
  ' its not there
Else
  ' it is there

If you just try to get the text without checking whether the node/attribute exists you'll get error 91 so you could trap for this if you want:

hth - if not pls post some code so we can see what you're using
Phil
Reply By: babloo81 Reply Date: 11/13/2003 9:43:11 AM
Hi,
Thanks for the response.
The problem I have is that, whenever I use "selectSingleNode". I might get the error "Object or With Block not Set". This happens when my XML does not match the XPath.
So, this code,
Set node = domDoc.selectSingleNode("<<your XPATH>>")
may give out an error before I proceed to
If node Is Nothing Then
   ' its not there
Else
   ' it is there

Thanks,
Babloo

Reply By: pgtips Reply Date: 11/13/2003 11:15:36 AM
Yes that error is error 91.  But you shouldn't get that error unless you try to access some property of the node/attribute without first checking that it exists.

Example:
domDoc.selectSingleNode("<<your XPATH>>").text will generate that error if the node/attrib doesn't exist, but
domDoc.selectSingleNode("<<your XPATH>>") will not error, it will just return Nothing.
Reply By: babloo81 Reply Date: 11/14/2003 12:28:03 AM
Hi Phil,

Thanks a lot for the help.
So, this works.
    Dim x1 As New MSXML.DOMDocument
    x1.loadXML ("<P><T a='1' n='ttt'/><T a='2' n='yyy'/></P>")
    MsgBox x1.selectSingleNode("//T[@a=4]") Is Nothing 'Returns False
    MsgBox x1.selectSingleNode("//T[@a=2]") Is Nothing 'Returns True

Wow.
Tanks again Phil.

Babloo.



Go to topic 6426

Return to index page 1006
Return to index page 1005
Return to index page 1004
Return to index page 1003
Return to index page 1002
Return to index page 1001
Return to index page 1000
Return to index page 999
Return to index page 998
Return to index page 997