chotai.mit
chotai.mit

Reputation: 253

MS XML selectNodes method not returning the expected nodes

Code in VB6(XML File)

My Xml file as follows

<book>
    <name>x1</name>
    <price>1<price>
    <name>x2</name>
    <name>x3</name>
    <price>2<price>
</book>

Root Node is Book

Set nodlist = xmlDoc.selectNodes("/Book/Price")

It Selects 1 Nodes Only, Rest Are ignored

And I want to get all the nodes named price.

Upvotes: 1

Views: 2251

Answers (2)

cblab
cblab

Reputation: 1935

1/ as JP figured out in comments of OP, your second price node isn't closed correctly.

The selectNodes() method is using a XPath selector (syntax, tutorial), right ?

2/ as Dimitre said, XPath is case sensitive, so /Book/Price is invalid if your actual elements are book and price. It should be /book/price.

However, if the first price node is returned to you, it's most likely that your selector is actually the well lower-cased /book/price, and probaby that you problem comes from the second price node which is not properly closed.

3/ Finally, are you sure that book is really the root node of your XML ? Don't you have any bookstore parent, or something?

If your selector has the form /book/price (with only one slash at first), it is an absolute path, then it should select all price elements for the selected book indeed, but only il the given book is the (only very single) root node (with no other book in your doc obviously, since you cannot have several roots in one document, and no parent at all).

Try this:

  • book/price should return all price nodes for all book nodes of the documents
  • price should return all price nodes whatever be them parent(s) node(s) in the whole documents

If you have another root, like bookstore, or library and want to use an absolute path, try:

  • /bookstore/book/price (or /library/book/price, or whatever your actual root node is)

Also, you can try to access relatively to your nodes using something like that, depending or where are you located in your XML tree:

  • //book/price
  • //price

Upvotes: 1

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

Set nodlist = xmlDoc.selectNodes("/Book/Price")

XPath is CAse senSitiVe -- there are no nodes named Book or Price in the provided XML document. Therefore the above method call must return an empty XmlNodeList.

You want:

Set nodlist = xmlDoc.selectNodes("/book/price")

Upvotes: 2

Related Questions