Select value of attribute in xml through ASP

Posted on 2006-04-27
Last Modified: 2013-11-19
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,

strDate = "4/18/2006"
intMaxEvents = 3

Set oXMLHTTP = Server.CreateObject("Microsoft.XMLDOM")
oXMLHTTP.async = False
<?xml version="1.0" encoding="UTF-8"?>
          <event date="4/17/2006">
          <event date="4/20/2006">
          <event date="4/17/2006">
               <description>Run Marathon</description>
          <event date="4/17/2006">
          <event date="4/20/2006">
               <description>Another Item on another day</description>


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
     Response.Write "</ul>"
     Response.Write "XML file did not load"
End If
Set oXMLHTTP = Nothing
Question by:tamilgirl77
    LVL 12

    Accepted Solution

    Like this:

    Response.Write "<li>" & oEventNodes.Item(i).SelectSingleNode("@date").Text & "</li>"
    LVL 15

    Expert Comment

    - 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

    LVL 12

    Expert Comment

    @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...
    LVL 15

    Expert Comment

    > Is there an advantage to doing it the way you suggested compared to the way I did it?


    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.

    - I've learned something
    - @tamilgirl77, please don't accept my answer over @peterxlane's as they're functionally identical

    Author Comment

    Thank you both for helping me out,

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa‚Ķ
    JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to count occurrences of each item in an array.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now