tamilgirl77
asked on
Select value of attribute in xml through ASP
Can you someone help with how to get the value of date to be displayed right before the description in the code below,
thank you,
xml:
<%
strDate = "4/18/2006"
intMaxEvents = 3
Set oXMLHTTP = Server.CreateObject("Micro soft.XMLDO M")
oXMLHTTP.async = False
<?xml version="1.0" encoding="UTF-8"?>
<calendar>
<events>
<event date="4/17/2006">
<description>Party</descri ption>
</event>
<event date="4/20/2006">
<description>Work</descrip tion>
</event>
<event date="4/17/2006">
<description>Run Marathon</description>
</event>
<event date="4/17/2006">
<description>Recover</desc ription>
</event>
<event date="4/20/2006">
<description>Another Item on another day</description>
</event>
</events>
</calendar>
ASP:
If oXMLHTTP.Load(Server.MapPa th("calend ar.xml")) Then
Set oEventNodes = oXMLHTTP.documentElement.s electNodes ("//event[ @date>='" & strDate & "']")
Response.Write "<ul>"
For i = 0 To (oEventNodes.Length - 1)
Response.Write "<li>" & oEventNodes.Item(i).Select SingleNode ("descript ion").Text & "</li>"
If i = (intMaxEvents - 1) Then Exit For
Next
Response.Write "</ul>"
Else
Response.Write "XML file did not load"
End If
Set oXMLHTTP = Nothing
%>
thank you,
xml:
<%
strDate = "4/18/2006"
intMaxEvents = 3
Set oXMLHTTP = Server.CreateObject("Micro
oXMLHTTP.async = False
<?xml version="1.0" encoding="UTF-8"?>
<calendar>
<events>
<event date="4/17/2006">
<description>Party</descri
</event>
<event date="4/20/2006">
<description>Work</descrip
</event>
<event date="4/17/2006">
<description>Run Marathon</description>
</event>
<event date="4/17/2006">
<description>Recover</desc
</event>
<event date="4/20/2006">
<description>Another Item on another day</description>
</event>
</events>
</calendar>
ASP:
If oXMLHTTP.Load(Server.MapPa
Set oEventNodes = oXMLHTTP.documentElement.s
Response.Write "<ul>"
For i = 0 To (oEventNodes.Length - 1)
Response.Write "<li>" & oEventNodes.Item(i).Select
If i = (intMaxEvents - 1) Then Exit For
Next
Response.Write "</ul>"
Else
Response.Write "XML file did not load"
End If
Set oXMLHTTP = Nothing
%>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@deighc - Is there an advantage to doing it the way you suggested compared to the way I did it? I work a fair amount with XSL, so it seemed intuitive for me to just provide an XPath allow with the SelectSingleNode method, but I am curious if your way may have some advantages that I am not aware of...
> Is there an advantage to doing it the way you suggested compared to the way I did it?
No.
It was my understanding that attributes could only be accessed programatically through the attributes collection. I didn't realise that they were also accessible by using the SelectSingleNode() method. But this makes sense since an XML parser builds a 'node tree' from all element and attribute nodes.
So:
- I've learned something
- @tamilgirl77, please don't accept my answer over @peterxlane's as they're functionally identical
No.
It was my understanding that attributes could only be accessed programatically through the attributes collection. I didn't realise that they were also accessible by using the SelectSingleNode() method. But this makes sense since an XML parser builds a 'node tree' from all element and attribute nodes.
So:
- I've learned something
- @tamilgirl77, please don't accept my answer over @peterxlane's as they're functionally identical
ASKER
Thank you both for helping me out,
- A node object has an attributes collection, and this collection has a getNamedItem method which returns an attribute object
- The attribute object has a value property
So, to access the 'date' attribute of the <event> node inside your For...Next loop try something like this
oEventNodes.item(i).atttri