472,096 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Xpath queries not working in some cases

Hi,

I am new in XML. But I have done coding in VBA, C, and HTML. I am using VBA to extract data from an xml file. I use Xpathbuilder to generate Xpath queries for my work. However, some Xpath queries are causing errors in my program. As I am new in xml and its standards I am not sure what is causing them. Answers to the following questions might help me in understanding these problems better. Can you please help me? Thanks.

1. Which version of xml am I using? In my program I declare variables as shown here. Dim oDoc As MSXML2.DOMDocument, Dim oNodeList As msxml2.IXMLDOMNodeList. Also, in Refrences of the VBA environment I have selected Microsoft xml.V6.0. I have Microsoft XP and Office 2003 SP2.

2. Why the SelectionLanguage property is not working? When I put the following statement in my code, the selectnodes statement gives me an error. Selectnodes work fine without this statement. I donít know why this is happening.
oDoc.setProperty "SelectionLanguage", "XPath"

3. Why the Selectnodes statement is not working for a few queries? I am using this statement:
Set oChildren = oDoc.selectNodes(Path). This statement works for simple queries such as
Path = /FSK:Research/FSK:Product/FSK:Context/FSK:IssuerDetails/FSK:Issuer/FSK:SecurityDetails/FSK:Security
But causes an error for
Path =
/FSK:Research/FSK:Product/FSK:Context/FSK:IssuerDetails/FSK:Issuer/FSK:SecurityDetails/FSK:Security [@securityID='1404']
Or Path =
/FSK:Research/FSK:Product/FSK:Context/FSK:IssuerDetails/FSK:Issuer/FSK:SecurityDetails/FSK:Security/FSK:FinancialGroup [2]

Note that I have verified these paths using Xpathbuilder.

I am also attaching my code below.

Thanks.
Expand|Select|Wrap|Line Numbers
  1. Dim oDoc As msxml2.DOMDocument
  2.         Dim fSuccess As Boolean
  3.         Dim oRoot As msxml2.IXMLDOMNode
  4.         Dim oBegining As msxml2.IXMLDOMNode
  5.         Dim oAttributes As msxml2.IXMLDOMNamedNodeMap
  6.         Dim oCountryName As msxml2.IXMLDOMNode
  7.         Dim oChildren As msxml2.IXMLDOMNodeList
  8.         Dim oChild As msxml2.IXMLDOMNode
  9.         Dim oNodeList As msxml2.IXMLDOMNodeList
  10.  
  11.         Dim Startcell As Range
  12.         Dim temp As String
  13.         Dim temp2 As String
  14.         Dim Colno As Integer
  15.         Dim oCell As Range
  16.         Dim Path As String
  17.         Dim Counter As Integer
  18.  
  19. On Error GoTo Exitfun
  20.  
  21.     Set oDoc = CreateObject("MSXML2.DOMDocument")
  22.  
  23.     oDoc.async = False
  24.     oDoc.validateOnParse = False
  25.  
  26.     oDoc.Load ("c:\test.xml")
  27.  
  28.   ' MsgBox oDoc.XML
  29.  
  30.     Worksheets("Template").Select
  31.  
  32.     Set oRoot = oDoc.documentElement
  33.  
  34.     Set oNodeList = oDoc.getElementsByTagName("FSK:SecurityName")  This works fine.
  35.  
  36.      temp = oNodeList.Item(0).childNodes.Item(0).Text
  37.  
  38.  
  39.     Set Startcell = ActiveCell
  40.     Counter = 0     ' to count columns
  41.  
  42.     For Each oCell In Worksheets("Instructions - Introduction").Range("G1:G21")
  43.  
  44.  
  45.     Path = oCell.Value
  46.  
  47.    Set oChildren = oDoc.selectNodes(Path) This doesn't work sometimes.
  48.  
  49.    Startcell.Offset(0, Counter).Value = oChildren.Length
  50.  
  51.     Counter = Counter + 1
  52.     Next
  53. Exit Sub
  54.  
  55. Exitfun: MsgBox "Error occured" & Counter
  56.  
  57. End Sub
  58. --------------------------------------------------------------------------------
  59. Last edited by musicgold
  60.  
Jan 11 '09 #1
3 4973
Dormilich
8,658 Expert Mod 8TB
1. XML version should be 1.0, but that should be declared in the file's xml prolog

3. I havn't found a good reference for selectNode() but it looks like there are problems if a condition is to fulfill in the XPath. another possibility could be that oDoc is not of the expected type (MDC says you need a Range object for that, don't know how it is handled on IE, though)

regards
Jan 11 '09 #2
jkmyoung
2,057 Expert 2GB
1&2&3. SelectionProperty Support. The XPath value is only supported in MSXML3.0 and up. You're using mostly MSXML2 objects. See: SelectionLanguage Property These probably don't support these particular xpaths.

Instead of using msxml2.IXMLDOMNode, why not just use IXMLDOMNode , etc.. for other objects?
Jan 12 '09 #3
Thanks folks. I found a solution for the problem. It was related to defining namespace. The error I was getting was - undefined namespace or something. So I inserted the following line in my code and it started working fine.

oDoc.setProperty "SelectionNamespaces", "xmlns:FSK='http://www.Trevis.com/xmlSchema/Tradepro'"

Also,

jkmyoung said:
Instead of using msxml2.IXMLDOMNode, why not just use IXMLDOMNode , etc.. for other objects?
I am new in xml. I don't know the advantage of one over the other. Could you point me to a webpage that explains the difference between these two types?
Jan 15 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Scott Simpson | last post: by
1 post views Thread by Petr Vlcek | last post: by
1 post views Thread by Robert Sander | last post: by
2 posts views Thread by Richard L Rosenheim | last post: by
8 posts views Thread by cerelaz | last post: by
4 posts views Thread by Sheldon | last post: by
reply views Thread by leo001 | last post: by

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.