XML / XPath query problem

Hello all,

I’ve been banging away at this problem for a while and I’m at my wit’s end… Doing a seemingly simple XPath query, and it never returns anything… I suspect it might have something to do with the SOAP envelope, but when I test in an online XPath tool, my query works fine. HUH?

Here’s the code snippet, along with the (long) XML payload. (Note: I’m using a hard-coded xml payload for now, I also tested with getting it from the actual webservice with the same result, so I don’t think it’s because I somehow broke the XML. XmLDocument decodes it fine, I’ve inspected it and everything is where it should be.)

[code]dim data as string = “<?xml version=""1.0"" encoding=""utf-8""?><soap:Envelope xmlns:soap=”“http://schemas.xmlsoap.org/soap/envelope/”" xmlns:xsi="“http://www.w3.org/2001/XMLSchema-instance”" xmlns:xsd="“http://www.w3.org/2001/XMLSchema”">soap:Body<GetNextTripsForStopResponse xmlns="“http://octranspo.com”"><StopNo xmlns="“http://tempuri.org/”">6908<StopLabel xmlns="“http://tempuri.org/”">HWY 417 GREENBANK<Error xmlns="“http://tempuri.org/”" /><Route xmlns="“http://tempuri.org/”">96HurdmanEastbound20131118200049Hurdman Downtown / Hurdman Ctre-ville19:224-1false6EB - 60Hurdman Downtown / Hurdman Ctre-ville20:02190.57false6EB - 6045.309696-75.9078790.5Hurdman Downtown / Hurdman Ctre-ville19:52380.54false6EB - 6045.248655-75.91101150.0</soap:Body></soap:Envelope>"

dim doc as XmlDocument= new XmlDocument
doc.LoadXml(data)
Dim nodes As XmlNodeList = doc.xql("//Trip")
dim f as integer = nodes.Length[/code]

As stated, nodes always remains empty, f is zero.

I’ve used XPath in another Xojo project recently and it worked fine. What am I missing ?

I’ve had a lot of trouble with xql and namespaces. At one point, I wrote a routine to strip all namespaces out of the xml.

Hi Phillipe,

I agree with Tim. There is something not quite being understood with the data (as string).

Dim data as string = "<?xml version=""1.0"" encoding=""UTF-8""?><Library><Book>Access for Xojo</Book><Book>Excel for Xojo</Book></Library>" Dim doc as XmlDocument= new XmlDocument doc.LoadXml(data) Dim nodes As XmlNodeList = doc.xql("//Book") msgbox cstr(nodes.Length)

When the above modified code is executed, the messagebox gives the correct answer of 2.

Maybe try on a smaller portion of XML data and see what may be giving you the error, such as namespaces. :slight_smile:

I possibly found an issue with the XML code. The nesting of the term ‘Error’ may be causing one of the errors :slight_smile:

I was messing around with the XML code, and some of it is modified (soap removed, etc.)

You have to tell the name space:

doc.loadXml(data)

//For the Trip node, the last name space declared is http://tempuri.org/ at the Route node level
dim map() as string=array("tempuri","http://tempuri.org/") 

dim nodes as xmlNodeList=doc.xql("//tempuri:Trip",map)

dim f as integer=nodes.length

[quote=47286:@Antonio Rinaldi]You have to tell the name space:

[code]
doc.loadXml(data)

//For the Trip node, the last name space declared is http://tempuri.org/ at the Route node level
dim map() as string=array(“tempuri”,“http://tempuri.org/”)

dim nodes as xmlNodeList=doc.xql("//tempuri:Trip",map)

dim f as integer=nodes.length
[/code][/quote]

Hmmm… I tried this and I got the dreaded “The application has stopped working” Windows dialog… I don’t even get an exception (I have “Break on exception” set in the IDE)

Oh kripes, now the entire IDE crashed on me while I was trying to monkey around with this namespace stuff.

I can’t explain it, but the following code works. I removed many of the xmlns="“http://tempuri.org/”" code and it now works. When I was holding down the right-arrow key within the IDE and looking for pieces of code in the long string, the cursor would stop after a certain cursor movement (maybe 20 or so). Lifting the cursor and holding the right-arrow key would cause the cursor to move another 20 or so lengths. Is this possibly related-or not? (Windows 8.1, Xojo 2013 r3.3)

Is there possibly a limit on the number of double-quotes allowed in a string?

Sincerely,

Eugene

dim data as string = "<?xml version=""1.0"" encoding=""utf-8""?><soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><soap:Body><GetNextTripsForStopResponse><GetNextTripsForStopResult><StopNo>6908</StopNo><StopLabel>HWY 417 GREENBANK</StopLabel><Error/><Route><RouteDirection><RouteNo>96</RouteNo><RouteLabel>Hurdman</RouteLabel><Direction>Eastbound</Direction><Error /><RequestProcessingTime>20131118200049</RequestProcessingTime><Trips><Trip><TripDestination>Hurdman Downtown / Hurdman Ctre-ville</TripDestination><TripStartTime>19:22</TripStartTime><AdjustedScheduleTime>4</AdjustedScheduleTime><AdjustmentAge>-1</AdjustmentAge><LastTripOfSchedule>false</LastTripOfSchedule><BusType>6EB - 60</BusType><Latitude /><Longitude /><GPSSpeed /></Trip><Trip><TripDestination>Hurdman Downtown / Hurdman Ctre-ville</TripDestination><TripStartTime>20:02</TripStartTime><AdjustedScheduleTime>19</AdjustedScheduleTime><AdjustmentAge>0.57</AdjustmentAge><LastTripOfSchedule>false</LastTripOfSchedule><BusType>6EB - 60</BusType><Latitude>45.309696</Latitude><Longitude>-75.907879</Longitude><GPSSpeed>0.5</GPSSpeed></Trip><Trip><TripDestination>Hurdman Downtown / Hurdman Ctre-ville</TripDestination><TripStartTime>19:52</TripStartTime><AdjustedScheduleTime>38</AdjustedScheduleTime><AdjustmentAge>0.54</AdjustmentAge><LastTripOfSchedule>false</LastTripOfSchedule><BusType>6EB - 60</BusType><Latitude>45.248655</Latitude><Longitude>-75.911011</Longitude><GPSSpeed>50.0</GPSSpeed></Trip></Trips></RouteDirection></Route></GetNextTripsForStopResult></GetNextTripsForStopResponse></soap:Body></soap:Envelope>" dim doc as XmlDocument= new XmlDocument doc.LoadXml(data) Dim nodes As XmlNodeList = doc.xql("//Trip") msgbox cstr(nodes.Length)

There is some trouble in the XQL and the name spaces
I have wrote the code in a window open event
In debug mode, step by step my suggestion works, but crashes on exit the event.

But if I add this
if f<>0 then
dim xx() as String
for i as integer=0 to f-1
xx.Append nodes.Item(i).ToString
next
end if
it doesn’t doesn’t crash

really strange.

btw all those xmlns=“http://tempuri.org/” are nonsense, they don’t define a real name space (i.e. xmlns:myspace=“http://tempuri.org/”), so since you start from a string I suggest to remove all of them and simplify the process

Thanks Eugene and Antonio, much appreciated, I’ll just write a regexp to strip that stuff out. I don’t have control of the webservice but I’ll massage their stuff to make it work.

I do know it’s not the double-quotes in the string - I use the string for testing and I had to add the double-quotes myself, but the problem is same when I use the actual WS and it doesn’t have double quotes.