Question

Ce que j'ai jusqu'à présent

Je crée un fil d'actualité pour mon site et je l'ai actuellement.

<%
TheFeed = "http://feeds.feedburner.com/Actsoft"

Set objXML = Server.CreateObject("Microsoft.XMLDOM")

objXML.Async = False
objXML.SetProperty "ServerHTTPRequest", True
objXML.ResolveExternals = True
objXML.ValidateOnParse = True
objXML.Load(TheFeed)

CellCount = 0

If (objXML.parseError.errorCode = 0) Then
   Set objRoot = objXML.documentElement
   If IsObject(objRoot) = False Then
       Response.Write "There was an error retrieving the news feed"
   Else
       Set objItems = objRoot.getElementsByTagName("item")
          If IsObject(objItems) = True Then
              For Each objItem in objItems
                  On Error Resume Next
                  TheTitle =  objItem.selectSingleNode("title").Text
                  TheLink =  objItem.selectSingleNode("link").Text

                  Response.Write "<div class='article'>" &_
                                 "<a href=" & TheLink & ">" & _
                                 "<span>" & TheTitle & "</span>" & _
                                 "</a>" & _
                                 "</div>"
             Next
         End If
     Set objItems = Nothing
   End If
Else
    Response.Write "There was an error retrieving the news feed"
End If
Set objXML = Nothing
%>

Ce dont j'ai besoin

Je souhaite limiter la quantité d'objets affichés dans mon lecteur.À l'heure actuelle, tous les articles sont affichés et je souhaite limiter en affichant uniquement les 4 premiers.

Je suis nouveau sur Asp donc je n'ai aucune idée de comment procéder.

Était-ce utile?

La solution

Peut être fait avec un compteur dans la boucle for mais j'aimerais utiliser XPath.

Définissez la langue de sélection sur XPath.

objXML.SetProperty "ServerHTTPRequest", True
objXML.SetProperty "SelectionLanguage", "XPath"

Sélectionnez les éléments avec

objXML.selectNodes("//item[position() <= 4]")

plutôt

objRoot.getElementsByTagName("item")

Ensuite, les quatre premiers seront affichés.

Une suggestion sur If IsObject(objItems) ... etc :

Les méthodes telles que getElementsByTagName, sélectionner des nœuds renvoie une collection d'éléments qui ont le nom/l'expression spécifié.Si aucun nœud ne correspond au nom/expression, renvoie une liste/collection vide et cela ne provoque pas d'erreur lorsque vous essayez de l'itérer de manière native (Pour chaque).

Mais certaines méthodes de sélection renvoient uniquement un objet nœud (selectSingleNode, obtenirNamedItem).Si aucun nœud ne correspond, renvoie Nothing.Le problème est que, Nothing est aussi un objet.Donc IsObject(Nothing) renvoie toujours vrai.
Dans de tels cas, vous pouvez gérer comme suit.

'On Error Resume Next
Set TheTitle = objItem.selectSingleNode("title")
Set TheLink = objItem.selectSingleNode("link")

If TheTitle Is Nothing Then TheTitle = "" Else TheTitle = TheTitle.Text
If TheLink Is Nothing Then TheLink = "" Else TheLink = TheLink.Text
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top