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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding unique value using Xpath

Status
Not open for further replies.

Idee

Programmer
Jul 8, 2003
51
NZ
Hi

The structure of my XML is

<Sales>
<sale>
<items>
<item>
<id>1</id>
<saleid>5</saleid>
</item>
<item>
<id>2</id>
<saleid>5</saleid>
</item>
</items>
</sale>
<sale>
<items>
<item>
<id>1</id>
<saleid>6</saleid>
</item>
<item>
<id>2</id>
<saleid>6</saleid>
</item>
<item>
<id>3</id>
<saleid>6</saleid>
</item>
</items>
</sale>
</sales>

Set obj = CreateObject("Msxml.domdocument")
obj.loadXML (xml)

I am looking for saleid in the xml

Set nodelist = obj.selectNodes("//saleid")

It returns me the length of nodelist as 5 because each item in xml has saleid but for each sale, there is same saleid. What I want is that it should return me unique saleid only i.e nodelist length should be 2 instaed of 5. I can not change the structure of my XML.

Then in VB, I loop through the nodes and pass saleid to another programe.

How do I loop through in VB and then get the value of saleid? I am doing

For i = 0 To i = (nodelist.length - 1)
SaleID = NodeValue
Next

There is something wrong in line
SaleID = NodeValue

Your help is much appreciated

Thanks
 
Code:
For i = 0 To i = (nodelist.length - 1)
  SaleID = nodelist.childNodes[i].nodeValue
Next
Or:
Code:
For Each x in nodelist
  SaleID = x.nodeValue
Next
But better than would be to use the correct XPath for the nodes you want. What determines the saleids that you are looking for? For instance, to pull out the saleid for the first sale list where the id is 2, you would use:
Code:
Set nodelist = obj.selectNodes("Sales/sale[1]/items/item[id=2]/saleid")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top