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

Xpath queries not working in some cases


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.

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
  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
  19. On Error GoTo Exitfun
  21.     Set oDoc = CreateObject("MSXML2.DOMDocument")
  23.     oDoc.async = False
  24.     oDoc.validateOnParse = False
  26.     oDoc.Load ("c:\test.xml")
  28.   ' MsgBox oDoc.XML
  30.     Worksheets("Template").Select
  32.     Set oRoot = oDoc.documentElement
  34.     Set oNodeList = oDoc.getElementsByTagName("FSK:SecurityName")  This works fine.
  36.      temp = oNodeList.Item(0).childNodes.Item(0).Text
  39.     Set Startcell = ActiveCell
  40.     Counter = 0     ' to count columns
  42.     For Each oCell In Worksheets("Instructions - Introduction").Range("G1:G21")
  45.     Path = oCell.Value
  47.    Set oChildren = oDoc.selectNodes(Path) This doesn't work sometimes.
  49.    Startcell.Offset(0, Counter).Value = oChildren.Length
  51.     Counter = Counter + 1
  52.     Next
  53. Exit Sub
  55. Exitfun: MsgBox "Error occured" & Counter
  57. End Sub
  58. --------------------------------------------------------------------------------
  59. Last edited by musicgold
Jan 11 '09 #1
3 5080
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)

Jan 11 '09 #2
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'"


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

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

Similar topics

by: Scott Simpson | last post by:
What is a good tool for running XPath queries on Linux? I have the O'Reilly XPath book and I'm using XPath Visualizer on Windows and that seems to work fine, but I'm looking for something on Linux....
by: rosemm | last post by:
i have a file which I get from a vendor. we are using a industry schema, hr-xml. however, I can't seem to get any XPath expresions to work when i receive the file. i think i have pinpointed the...
by: Petr Vlcek | last post by:
Hello, I am engaded in optimization of XPath queries as a topic of my diploma thesis. Our team is developing experimental system for indexing of XML document collections. My part is to develop...
by: Echo 8 | last post by:
Is there a simple way of compare dates? I am trying to create a list of perishable stock in an inventory that is near or past its expiration date. I've tried telling it to compare a node's value...
by: Robert Sander | last post by:
Hi, I have to do some case-insensitive queries on a XML document using XPath. Because XML 1.0 and XPath 1.0 are case-sensitive this is a little bit tricky. Sample document: <books>...
by: Richard L Rosenheim | last post by:
Anyone care to recommend a web site that has some really good examples of XPath queries utilizing attributes? My XML books, and the web sites that I've looked at, don't really utilize...
by: Joost Kraaijeveld | last post by:
Hi all, In my application that is generated by Clarion an SQL0418N ("A statement contains a use of a parameter marker that is not valid.") occurs. In the explanation section it says "in some...
by: cerelaz | last post by:
Hi, I need to do some Xpath queries. How to add (or multiply) a values returned by a const? why this //book/price/text()*10 doesn't work? what's wrong? thanks ps I need to use a great c++...
by: Sheldon | last post by:
Hi, I have a unique case where I need an array of structs that grows and within this array is another struct that grows in some cases. I'm having trouble allocating memory. Since I have never...
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...
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...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
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...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.