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

XML node exists - ASP question

Status
Not open for further replies.

jez

Programmer
Apr 24, 2001
370
VN
OK, this is an ASP question despite the title.


I am trying to check whether an element exists in an XML page.

I can do it in other languages but i need to use asp for the site i am working on.

Having used an XQL query to return a set of elements I now want to get the attribute of an element which is not always present in the supplied (3rd party) XML.

Code:
if isNull(horse.selectNodes("LastRunDays"))=false then 
response.write horse.selectNodes("LastRunDays").getAttribute("days") 
else response.write "error" 
end if

but this gives the error

Object doesn't support this property or method: 'getAttribute'

which was the same as when i just tried to access the atrribute without checking for the element first.

Any help, please?

JEz
 
have you tried isObject()?

if isObject(horse.selectNodes("LastRunDays")) then
response.write horse.selectNodes("LastRunDays").getAttribute("days")
else response.write "error"
end if


Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
The problem is you are selecting multiple nodes. The select nodes method returns an element list, not an element. The getAttribute method belongs to the element object. In order to check you would need to either:
a) use .SelectSingleNode in place of SelectNodes to select a single element and then pull out the attribute
or
b) Find another way to do this

You may want to try soething simple like a .Count. I haven't tried it recently (been dealing with to many differant languages), but the element list may support a .Count or a .length property so you could simply do:
Code:
If horse.SelectNodes("LastRunDays").Count = 0 Then
   'no last run days
End If

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
Hi guys,

Thanks for this but infortunately neither work.

When i either selectNodes("") or selectSingleNode("") either a count or length property fails as there is no object to count.

Also the isObject seems to find an object no matter what and gives the following error.

Object required: 'horse.selectSingleNode(...)'


to give some background the horse object is a set of nodes that are being looped thorough following an xql query.

I will keep trying, but i am very surprised there is no method in ASP to do something so obvious.

Thanks again,

JEz
 
According to the definition, the NodeList object is supposed to have a .Length attribute, so:
Code:
If horse.SelectNodes("LastRunDays").Length > 0 Then
should have worked. Even if this does work, the internal portion will fail because you can't get an attribute fro a group of nodes, only from a single node, so you will have to use a for loop inside the if statement to loop through all nodes in the NodeList and getAttribute from each one.

Since this method is failing for you then undoubtedly there is something wrong earlier (I just tested this on a piece of sample code form another post and it worked fine).

Your final code should look something like:
Code:
Dim ld
If horse.SelectNodes("LastRunDays").Length > 0 Then
   For Each ld in horse.SelectNodes("LastRunDays")
      Response.Write d.getAttribute("days")
   Next
End If

This assumes that a horse can have multiple LastRunDays which can hawill each have a day. If thats the case then you don't really need the If statement, the For Each will take care of business for you.
If you can't have multiples, than I would use the If statement to chack that there are more than 0 then make calls to SelectSingleNode inside the if statement because a NodeList (which is what is returned from the .selectNodes method) doesn't have a getAttribute function:
Code:
If horse.SelectNodes("LastRunDays").Length > 0 Then
   Response.Write horse.SelectSingleNode("LastRunDays").getAttribute("days")
End If

The above code wil still give you nasty errors if you have any element LastRunDays that doesn't have thwe attribute days.




01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top