DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2009
    Posts
    8

    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.


    Code:
     Dim oDoc As msxml2.DOMDocument
            Dim fSuccess As Boolean
            Dim oRoot As msxml2.IXMLDOMNode
            Dim oBegining As msxml2.IXMLDOMNode
            Dim oAttributes As msxml2.IXMLDOMNamedNodeMap
            Dim oCountryName As msxml2.IXMLDOMNode
            Dim oChildren As msxml2.IXMLDOMNodeList
            Dim oChild As msxml2.IXMLDOMNode
            Dim oNodeList As msxml2.IXMLDOMNodeList
            
            Dim Startcell As Range
            Dim temp As String
            Dim temp2 As String
            Dim Colno As Integer
            Dim oCell As Range
            Dim Path As String
            Dim Counter As Integer
    
    On Error GoTo Exitfun
        
        Set oDoc = CreateObject("MSXML2.DOMDocument")
        
        oDoc.async = False
        oDoc.validateOnParse = False
        
        oDoc.Load ("c:\test.xml")
    
      ' MsgBox oDoc.XML
            
        Worksheets("Template").Select
        
        Set oRoot = oDoc.documentElement
    
        Set oNodeList = oDoc.getElementsByTagName("FSK:SecurityName") This works fine.
    
         temp = oNodeList.Item(0).childNodes.Item(0).Text
         
             
        Set Startcell = ActiveCell
        Counter = 0     ' to count columns
        
        For Each oCell In Worksheets("Instructions - Introduction").Range("G1:G21")
        
            
        Path = oCell.Value
            
       Set oChildren = oDoc.selectNodes(Path) This doesn't work sometimes.
            
       Startcell.Offset(0, Counter).Value = oChildren.Length
        
        Counter = Counter + 1
        Next
    Exit Sub
    
    Exitfun: MsgBox "Error occured" & Counter
    
    End Sub

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,651
    Quote Originally Posted by musicgold
    2. Why the SelectionLanguage property is not working? When I put the following statement in my code, the selectnodes statement gives me an error.
    What are the errors that you are getting?
    Quote Originally Posted by musicgold
    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]
    Again, what is the error?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Jan 2009
    Posts
    8
    Hack,

    Thanks. Your questions prompted me to look at he error messages. The error 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'"

    Thanks again.

  4. #4
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,651
    You are welcome.

    Sometimes prompting is as good as answering.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

Similar Threads

  1. Xpath queries with SAX
    By Jaco De Villiers in forum XML
    Replies: 2
    Last Post: 05-27-2010, 01:22 PM
  2. Combining three queries into one
    By umamahi in forum Database
    Replies: 1
    Last Post: 01-30-2007, 09:53 PM
  3. SQL Tutorial (Answer Q's & post your reply)
    By bigbastard4 in forum Database
    Replies: 2
    Last Post: 05-16-2001, 06:24 PM
  4. Queries on Multiple Tables
    By Andrew Beaton in forum Database
    Replies: 0
    Last Post: 12-11-2000, 06:32 AM
  5. VSS:sharing same working folder without multiple checkouts
    By rahul phatak rahulphatak in forum ASP.NET
    Replies: 0
    Last Post: 06-01-2000, 05:41 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links