.NET Framework - Xpath - selectSingleNode -[starts-with??`

Asked By Dave F. on 30-Sep-07 05:28 PM

Is there away to use starts-with with selectSingleNode?

I have am XML file of lots of random numbers:


I'm using vb(a) & this works:

Dim NodeLst As IXMLDOMNodeList
OffsetNo = 2
Set NodeLst = MsDom.selectNodes("/Offset/Dist/x[starts-with(.,'" &
OffsetNo & "')]")

But i'd like to use selectSingleNode so I can place the result in a
Listbox using this sort of routine:

Set Child = NodeLst.firstChild
While Not Child Is Nothing
LstBxRecent.AddItem Child.Text
Set Child = Child.nextSibling

Is this possible? If so please help me out because I'm stuck.

Dave F.

TOUDIdel replied on 01-Oct-07 02:06 AM
Uzytkownik "Dave F." <df@maaf.cu.uk> napisal w wiadomosci

Generally all XPath functions work in selectNodes() method, but sometimes
(e.g. JScript) you have to set special property:

Martin Honnen replied on 01-Oct-07 08:49 AM
Yes, but with MSXML 3 you need to set
MsDom.setProperty "SelectionLanguage", "XPath"
first. Later MSXML version (e.g. 4/5/6) do support XPath by default.


Martin Honnen --- MVP XML
Dave F. replied on 01-Oct-07 01:06 PM
Thanks to both for your replies.
I'm using v4, so it must be a problem with the syntax. Which is what's
confusing me.
It returning just the first node it finds. I thought it returned a node
with all the test strings beginning with 5.

You'll have to excuse me, I'm newish to XML/XPATH. & not sure what I'm

Can you point me in the right direction?

Dave F.
Joe Fawcett replied on 01-Oct-07 04:26 PM
If it works with SelectNodes then it's the same with SelectSingleNode, which
just delegates to SelectNodes and returns the first in the list.
The Offset you specified was 2, not 5, the XPath should look like:
/Offset/Dist/x[starts-with(., '2')]
so make sure that's what you get my using a MsgBox or similar to display the
final XPath before using it.


Joe Fawcett (MVP - XML)

Brian Staff replied on 01-Oct-07 04:43 PM

suggestion...recode this:

Set NodeLst = MsDom.selectNodes("/Offset/Dist/x[starts-with(.,'" &
OffsetNo & "')]")

to this:

dim xpath
xpath = "/Offset/Dist/x[starts-with(.,'" &
OffsetNo & "')]"
' now you can see what xpath has evaluated to...
Set NodeLst = MsDom.selectNodes(xpath)

Another thing...is the node Offset your root node?

If so, you do not need the first slash.
xpath = "Offset/Dist/x[starts-with(.,'" &
OffsetNo & "')]"