• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 653
  • Last Modified:

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("Microsoft.XMLDOM")
oXMLHTTP.async = False
<?xml version="1.0" encoding="UTF-8"?>
<calendar>
     <events>
          <event date="4/17/2006">
               <description>Party</description>
          </event>
          <event date="4/20/2006">
               <description>Work</description>
          </event>
          <event date="4/17/2006">
               <description>Run Marathon</description>
          </event>
          <event date="4/17/2006">
               <description>Recover</description>
          </event>
          <event date="4/20/2006">
               <description>Another Item on another day</description>
          </event>
     </events>
</calendar>

ASP:

If oXMLHTTP.Load(Server.MapPath("calendar.xml")) Then
     Set oEventNodes = oXMLHTTP.documentElement.selectNodes("//event[@date>='" & strDate & "']")
     Response.Write "<ul>"    
     For i = 0 To (oEventNodes.Length - 1)
          Response.Write "<li>" & oEventNodes.Item(i).SelectSingleNode("description").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
%>
0
tamilgirl77
Asked:
tamilgirl77
  • 2
  • 2
1 Solution
 
peterxlaneCommented:
Like this:

Response.Write "<li>" & oEventNodes.Item(i).SelectSingleNode("@date").Text & "</li>"
0
 
deighcCommented:
- oEventNodes is a collection of <event> node objects.
- 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).atttributes.getNamedItem("date").value
0
 
peterxlaneCommented:
@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...
0
 
deighcCommented:
> 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
0
 
tamilgirl77Author Commented:
Thank you both for helping me out,
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now