XML XQL returns no nodes

I’ve got this xml node…

For even more space to unwind, the Corner Suite King offer a separate bedroom and living space. Decorated in a contemporary Thai style with a stunning city view from the large corner windows, these charming rooms are well outfitted with useful amenities such as WiFi Internet and a DVD player. Includes a deluxe bathroom.

Checking the query “/RoomStays/RoomStay/RoomTypes” with online XQuery tester (http://www.xpathtester.com/xquery) I get two nodes

But when trying to parse it in Xojo with XQL The same expression “/RoomStays/RoomStay/RoomTypes” returns zero nodes in the nodelist…

This is the line of code…
dim RoomNodeList as XmlNodeList = ReservationNode.Xql("/RoomStays/RoomStay/RoomTypes")

What am I doing wrong ?

You don’t have to insert the first / it means starting from root, you are evaluating a relative XQL from your reservation node.

Try:
dim RoomNodeList as XmlNodeList = ReservationNode.Xql(“RoomStays/RoomStay/RoomTypes”)

You will have 2 nodes

Hey Antonio thanks a lot for your reply…

I did exactly what you suggested… But the length of the resulting XmlNodelist is still Zero…

Here’s the Node I am working with (I’ve cut some repeated parts)…

For even more space to unwind, the Corner Suite King offer a separate bedroom and living space. Decorated in a contemporary Thai style with a stunning city view from the large corner windows, these charming rooms are well outfitted with useful amenities such as WiFi Internet and a DVD player. Includes a deluxe bathroom.

…changed the subject name, it was too vague…

Works for me. I saved your last example as atest.xml on the desktop, ran this code, and it reports: 2.

[code]dim f As FolderItem = SpecialFolder.Desktop.Child(“atest.xml”)

dim doc As new XmlDocument(f)

dim nl As XmlNodeList = doc.xql("/RoomStays/RoomStay/RoomTypes")

MsgBox Str(nl.Length)[/code]

I get 2 as well.

I tried again… I still get zero… (?!)

I just noticed in the debugger that the xml node I am trying to process with Xql is not an XMLNode but an XMLElement… (though I dimmed it as XmlNode)… Could this be related ? I am clueless…

What is your full code? I’m not sure what you mean by “I dimmed it as XmlNode”.

And did you try my code?

XMLElement is a kind of XMLNode, so no problem there. What is the Name property of the node you’re trying to process?

@Tim the Name property of the node I am trying to process is RoomStays

Will I’ll try your code in aminute and let you know how it goes…

Thanks guys for the help!

Now… this is strange…
IF I load the file from the desktop as in will’s test I get a length property = 2… but If I do it from exactly the same xml document from the current method the lenght property is ZERO !

My code has nothing strange…

/// ‘NodoReservaRoomStays’ contains the same XML than the file ‘atestnew.xml’

dim testXQL as XmlNodeList = NodoReservaRoomStays.Xql("/RoomStays/RoomStay/RoomTypes")

dim f As FolderItem = SpecialFolder.Desktop.Child("atestnew.xml")

dim doc As new XmlDocument(f)

dim nl As XmlNodeList = doc.xql("/RoomStays/RoomStay/RoomTypes")

I could upload a small version of the app for you to test it if you can/think it’s worth it…

I tried saving what the app has in memory and re loading it, and I get a different result…Maybe this is a bug… It doesn’t make any sense at all !

/// THE NODE I AM CHECKING IS NodeReservaRoomStays

dim testXQL as XmlNodeList = NodeReservaRoomStays.Xql("/RoomStays/RoomStay/RoomTypes") // [b]length property = 0

[/b]

/// SAVE NodeReservaRoomStays.ToString

Dim f As FolderItem
Dim fileStream as TextOutputStream
f=GetSaveFolderItem(".xml","CapturedXML.xml")

If f <> Nil then
  fileStream=TextOutputStream.Create(f)
  fileStream.Write NodeReservaRoomStays.ToString
  fileStream.Close
End if

/// RELOAD THE SAVED XML FILE

dim loadedXML As FolderItem = SpecialFolder.Desktop.Child("CapturedXML.xml")

dim doc As new XmlDocument(loadedXML)

dim nl As XmlNodeList = doc.xql("/RoomStays/RoomStay/RoomTypes") // [b]length property = 2 ?!?!?[/b]

I’d like to see an example. I’ve not had a problem using XQL on xml I create in code, node by node.

As I said before:
NodeReservaRoomStays is as Node not the document (so it has a name)
If you start you XQL string with / you are asking not for the child nodes…

you can (and is more efficient) do query XQL starting from a Node.

Now: if NodeReservaRoomStays is the RoomStays node then your query has to be:

dim testXQL as XmlNodeList = NodeReservaRoomStays.Xql(“RoomStay/RoomTypes”)

One thought: ToString removes extraneous spaces. If you had a trailing space in a node name, the XQL would fail, but since the space would be removed by ToString, it would succeed after reloading.

Antonio, it works with and without the leading slash. The node is also the document root, so it works (or fails) both ways.

A node name cannot contain a space (it’s what divide the node name from possible attribute names…)

since is “indifferent” use or not / is better not to use it since is more clear (you can see it as a Path…)

The problem here is that Doc and DocumentElement are always two distinct object:
Roman says:
dim nl As XmlNodeList = doc.xql("/RoomStays/RoomStay/RoomTypes") // length property = 2 ?!?!?
and
dim testXQL as XmlNodeList = NodeReservaRoomStays.Xql("/RoomStays/RoomStay/RoomTypes") // length property = 0

as if NodeReservaRoomStays and doc is the same object.
doc.firstChild == doc.documentElement == NodeReservaRoomStays

More over Roman has to check if there are NameSpace involved (since in a previous question he had troubles with them in a more complete document)

Yes… Antonio is right…

Changing the query to dim testXQL as XmlNodeList = NodoReservaRoomStays.Xql(“RoomStay/RoomTypes”) made all the difference

This is something that goes a bit beyond my skills… But my theory is that reloading the xml from the file creates a new xml document while the query I was trying to process was being performed on a node. That makes a differecne and after Anotnio’s commet it seems to make sense.

The issue about the namespace I fixed by removing the ns definition with a replaceall command over the string… Yes, it’s not nice… but I didn’t know how to do it properly… It’s a long XML to parse, and I thought it wouldn’t be such a steep learning curve…

Thanks a lot you guys, Antonio, Tim, Will… you have been very helpful… and probably it’s not over yet… :slight_smile: