reading XML

Discussion in 'ASP General' started by davidjohnlong@googlemail.com, Jul 4, 2006.

  1. Guest

    Hi,

    I'm currently trying to display a XML file within a ASP page, although
    get the following error when trying to select one of the notes... the
    node is called: <content:encoded>

    This is the error
    Reference to undeclared namespace prefix: 'content'.

    This is my code

    <%@ Language="VBScript" %>
    <html lang="en">
    <head>
    <title>Some feeds</title>
    <script runat="server" language="VBScript">
    Sub OutputFeed (feedURL)
    Dim XmlDocument
    Set XmlDocument = Server.CreateObject("Msxml2.DOMDocument.4.0")
    XmlDocument.setProperty "ServerHTTPRequest", true
    XmlDocument.async = False
    Dim Loaded
    Loaded = XmlDocument.load(feedURL)
    If Loaded Then
    Dim Items, Item, Title, Link, Description, TitleText,
    DescriptionText, LinkURL, Content2, Contenttext
    Set Items = XmlDocument.selectNodes("//item")
    Response.Write "<p>Feed from " & feedURL & " has currently " &
    Items.Length & " items.</p>" & VbCrLf
    Response.Write "<ul>" & VbCrLf
    For Each Item in Items
    Set Title = Item.selectSingleNode("title/text()")
    If Title Is Nothing Then
    TitleText = ""
    Else
    TitleText = Title.data
    End If
    Set Link = Item.selectSingleNode("link/text()")
    If Link Is Nothing Then
    LinkURL = ""
    Else
    LinkURL = Trim(Link.data)
    End If
    Set Content2 = Item.selectSingleNode("content:encoded")
    If Content2 Is Nothing Then
    Contenttext = ""
    Else
    Contenttext= Trim(Content2.data)
    End If
    Set Description = Item.selectSingleNode("description/text()")
    If Description Is Nothing Then
    DescriptionText = ""
    Else
    DescriptionText = Description.data
    End If
    Response.Write "<li><a href=""" & LinkURL & """>" & TitleText &
    ": " & DescriptionText & "</a></li>" & VbCrLf
    response.Write contexttext
    Next
    Response.Write "</ul>" & VbCrLf
    End If
    End Sub
    </script>
    </head>
    <body>

    <h1>Some feeds</h1>

    <%
    OutputFeed("http://www.biblegateway.com/usage/votd/rss/votd.rdf?31")
    OutputFeed("http://rss.news.yahoo.com/rss/elections")
    OutputFeed("http://www.cbsnews.com/feeds/rss/main.rss")
    OutputFeed("http://rss.cnn.com/rss/si_topstories.rss")
    %>

    </body>
    </html>


    Thank you for your help
    , Jul 4, 2006
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > Hi,
    >
    > I'm currently trying to display a XML file within a ASP page, although
    > get the following error when trying to select one of the notes... the
    > node is called: <content:encoded>
    >
    > This is the error
    > Reference to undeclared namespace prefix: 'content'.
    >


    The problem is that the document namespaces and the selection namespaces are
    separate.

    In order to select a node in a another namespace you need to add the
    namespace to the selection namespaces.

    This is done through a second level property called, believe it or not,
    "SelectionNamespaces"

    Assuming you have in the document the an alias declared like this:-

    <someToplevelNode xmlns:content="urn:thingy:stuff">

    or

    <someToplevelNode xmlns:content="http://somecompay.com/ournamespace">

    then your code should contain this before you attempt the select:-

    XmlDocument.setProperty "SelectionNamespaces",
    "xmlns:content='http://somecompay.com/ournamespace'"


    > This is my code
    >
    > <%@ Language="VBScript" %>
    > <html lang="en">
    > <head>
    > <title>Some feeds</title>
    > <script runat="server" language="VBScript">
    > Sub OutputFeed (feedURL)
    > Dim XmlDocument
    > Set XmlDocument = Server.CreateObject("Msxml2.DOMDocument.4.0")
    > XmlDocument.setProperty "ServerHTTPRequest", true
    > XmlDocument.async = False
    > Dim Loaded
    > Loaded = XmlDocument.load(feedURL)
    > If Loaded Then
    > Dim Items, Item, Title, Link, Description, TitleText,
    > DescriptionText, LinkURL, Content2, Contenttext
    > Set Items = XmlDocument.selectNodes("//item")
    > Response.Write "<p>Feed from " & feedURL & " has currently " &
    > Items.Length & " items.</p>" & VbCrLf
    > Response.Write "<ul>" & VbCrLf
    > For Each Item in Items
    > Set Title = Item.selectSingleNode("title/text()")
    > If Title Is Nothing Then
    > TitleText = ""
    > Else
    > TitleText = Title.data
    > End If
    > Set Link = Item.selectSingleNode("link/text()")
    > If Link Is Nothing Then
    > LinkURL = ""
    > Else
    > LinkURL = Trim(Link.data)
    > End If
    > Set Content2 = Item.selectSingleNode("content:encoded")
    > If Content2 Is Nothing Then
    > Contenttext = ""
    > Else
    > Contenttext= Trim(Content2.data)
    > End If
    > Set Description = Item.selectSingleNode("description/text()")
    > If Description Is Nothing Then
    > DescriptionText = ""
    > Else
    > DescriptionText = Description.data
    > End If
    > Response.Write "<li><a href=""" & LinkURL & """>" & TitleText &
    > ": " & DescriptionText & "</a></li>" & VbCrLf
    > response.Write contexttext
    > Next
    > Response.Write "</ul>" & VbCrLf
    > End If
    > End Sub
    > </script>
    > </head>
    > <body>
    >
    > <h1>Some feeds</h1>
    >
    > <%
    > OutputFeed("http://www.biblegateway.com/usage/votd/rss/votd.rdf?31")
    > OutputFeed("http://rss.news.yahoo.com/rss/elections")
    > OutputFeed("http://www.cbsnews.com/feeds/rss/main.rss")
    > OutputFeed("http://rss.cnn.com/rss/si_topstories.rss")
    > %>
    >
    > </body>
    > </html>
    >
    >
    > Thank you for your help
    >
    Anthony Jones, Jul 4, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page