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

select DISTINCT items from XML using ASP

Status
Not open for further replies.

ahfook

Programmer
Aug 5, 2002
2
0
0
MY
hello,
May I know how to select distinct items from a XML file? For example, I have a XML file contains the list of hotels in different countries, how do I select the distinct country from XML just like 'select distinct country from tblhotel' in SQL using ASP?
I've found the code select the distinct country using XSLT with 'preceding-sibling', but it just doesn't work with ASP...any idea?

my example XML file:
<hotels>

<hotel id=&quot;1&quot;>
<hotelname>Hotel 1</hotelname>
<country>USA</country>
<state>state1</state>
</hotel>

<hotel id=&quot;2&quot;>
<hotelname>Hotel 2</hotelname>
<country>UK</country>
<state>state2</state>
</hotel>

<hotel id=&quot;3&quot;>
<hotelname>Hotel 3</hotelname>
<country>USA</country>
<state>state3</state>
</hotel>

<hotel id=&quot;4&quot;>
<hotelname>Hotel 4</hotelname>
<country>China</country>
<state>state4</state>
</hotel>

<hotel id=&quot;5&quot;>
<hotelname>Hotel 5</hotelname>
<country>UK</country>
<state>state5</state>
</hotel>

<hotel id=&quot;6&quot;>
<hotelname>Hotel 6</hotelname>
<country>Japan</country>
<state>state6</state>
</hotel>

</hotels>

and the result I desire is: USA, UK, China, Japan

Thanks for help...

 
Can you post the code you found.

Jordi Reineman
 
what type of error are you getting in asp? what's your code in asp?

do you want to get a node-set from an xpath expression?

maybe something like the following xpath expression should work if you first sort them in order of state name

xmlobject.selectNodes
----------------------------------------
set xmlDoc=CreateObject(&quot;Microsoft.XMLDOM&quot;)
xmlDoc.async=&quot;false&quot;
xmlDoc.load(&quot;hotelfile.xml&quot;)

path = &quot;/hotels/hotel[preceding-sibling::state != state]/state&quot;

set nodes = xmlDoc.selectNodes(path)

for each x in nodes
document.write(&quot;<xmp>&quot;)
document.write(x.xml)
document.write(&quot;</xmp>&quot;)
next
--------------------------------------------
that would return a node-set of all of the hotel's state's whose previous hotel's state's are not the same mike griffith
----------------------------
mgriffith@lauren.com
mdg12@po.cwru.edu
 
Hai mgriffith, thanks for replying, here is my ASP code:

<%
set xmlcountry = server.createobject(&quot;microsoft.xmldom&quot;)
xmlcountry.async = false
xmlcountry.load(&quot;hotel.xml&quot;)
strpath = &quot;/hotels/hotel[preceding-sibling::country != country]/country&quot;
Set clist = xmlcountry.SelectNodes(strpath)
for i = 0 to clist.length - 1
response.write clist.item(i).text & &quot;<br>&quot;
next
%>

and the error I got is &quot;Unspecified error&quot;, error code '80004005'. The error is occured at line:
&quot;Set clist = xmlcountry.SelectNodes(strpath)&quot;.

I think it's XML syntax error and not ASP error...not so sure bcoz I just try to learn XML lately...

Lots of thanks,
ahfook
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top