Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VFP and XPath expressions

Status
Not open for further replies.

ChrisRChamberlain

Programmer
Mar 23, 2000
3,392
GB
Hi all

It appears that VFP may have a problem evaluating XPath expressions and I hope someone will prove otherwise.

The line of code in question is
Code:
FOR EACH objNode in objDocSelectNodes("//a[string(@href) != '']"
which should return a subset of nodes in accordance with the filter expression.

The following code snippit to be found at XStandard.com

Code:
<%
Dim objHTTP, objDoc, objNode
Set objDoc = Server.CreateObject("MSXML2.DOMDocument.4.0")
objDoc.Async = False
Set objHTTP = Server.CreateObject("XStandard.HTTP")
objHTTP.AddRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MyApp 1.0; Windows NT 5.1)"
objHTTP.Get "[URL unfurl="true"]http://xstandard.com"[/URL]
If objDoc.LoadXML(objHTTP.ResponseAsXML) Then
    For Each objNode In objDoc.SelectNodes("//a[string(@href) != '']")
        Response.Write "<div>" & objNode.Attributes.GetNamedItem("href").Text & "</div>
    Next
Else
    Response.Write "Cannot parse response."
End If
Set objHTTP = Nothing
Set objDoc = Nothing
Set objNode = Nothing
%>
The VFP code is
Code:
[COLOR=blue]oDoc    = CREATEOBJECT([MSXML2.DOMDocument.4.0])
oDoc.Async = .F.

oHTTP = CREATEOBJECT([XStandard.HTTP])
oHTTP.AddRequestHeader([User-agent],[Mozilla/4.0 (compatible; MyApp 1.0; Windows NT 5.1)])
oHTTP.Get([[URL unfurl="true"]http://xstandard.com[/URL]])

IF oDoc.LoadXML(oHTTP.ResponseAsXML)
    [/color][COLOR=red]FOR EACH objNode IN oDoc.SelectNodes("//a[string(@href) != '']")[/color][COLOR=blue]
        WAIT WINDOW objNode.Attributes.GetNamedItem("href").Text
    NEXT
ENDI    

objNode = .NULL.
oDoc = .NULL.
oHTTP = .NULL.[/color]
It appears that VFP cannot evaluate this particular expression, the error reported is 'Expression evaluator failed (Error 67)', and you get the same results even if you reduce the expression to something as minimal as ("//TITLE").

Tried all the available combinations of string delimiters and building strings using the VFP CHR() functions without success, even using simple expressions as posted.

TIA



FAQ184-2483 - answering getting answered.​
Chris [pc2]
PDFcommandertm.net
PDFcommandertm.co.uk
 
Does this mean that you have your solution, Chris?

I think that the expression that VFP is having trouble evaluating isn't the xpath expression, but the FOR EACH..IN expression.

Try using something like:
Code:
oNodes = oDoc.SelectNodes("//a[string(@href) != '']")
FOR i = 1 to oNodes.Count
  objNode = oNodes(i)
ENDFOR

- Bill

Get the best answers to your questions -- See FAQ481-4875.
 
Bil

Thanks for your response - no solution as yet.

There is a parallel thread333-1023839 in the ASP forum looking at the problem from an ASP perspective.

If you examine the DOMDocument class in Microsoft XML, version 2, in the VFP Object Browser, there is no .NodeCount property, .NodeCount() method or anything apparent which would enable you to establish the total nodes.


FAQ184-2483 - answering getting answered.​
Chris [pc2]
PDFcommandertm.net
PDFcommandertm.co.uk
 
I had just been going from memory.. I looked back and what I had done was using XPath in an XLST, like:
< xsl:element name="VFPDATA">
< xsl:for-each select="/response/event_report/event">
< events>
to extract particular nodes and convert the XML into VFP's schema for storing a cursor. Then I just used XmlToCursor and processed it from there... so VFP wasn't dealing directly with the Xpath.

I just fiddled with it and found that the name in MSXML v2 is "Length", not some permutation of "count", that tells you the number of nodes in the collection resulting from selectNodes.



- Bill

Get the best answers to your questions -- See FAQ481-4875.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top