SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    Kentucky USA
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Displaying only top five items

    Please can someone show me how can I change the following code to only show the top 5 or whatever number of items I need from the feed ?The following code works , but it displays the whole file. Only one day spent so far on this problem and hair here is in short supply.
    Thanks in advance...

    Sample asp web page....
    <%
    myXMLfile = "http://www.24-7pressrelease.com/rss/law_and_legal.xml"

    'Set getPage = Server.CreateObject("MSXML2.ServerXMLHTTP.4.0")
    'Set getPage = Server.CreateObject("Microsoft.XMLHTTP")
    Set getPage = Server.CreateObject("MSXML2.ServerXMLHTTP")

    getPage.Open "GET", myXMLfile, false
    getPage.SetRequestHeader "Content-type", "text/xml"
    getPage.Send

    'response.write getPage.responseXML.xml '' successfully outputs xml

    'Load XSL
    set xsl = Server.CreateObject("Microsoft.XMLDOM")
    xsl.async = false
    xsl.load(Server.MapPath("sample.xsl"))

    Function RightTrimLen(theStr, leftPadding)
    RightTrimLen = Right(theStr, (Len(theStr) - Len(leftPadding)))
    End Function
    ' above function is used to get rid of encoding that is added (and causing an error)

    theOutput = getPage.responseXML.transformNode(xsl)
    theOutput = RightTrimLen(theOutput, "<?xml version=""1.0"" encoding=""UTF-16""?>")

    response.write theOutput

    Set getPage = Nothing
    %>


    "sample.xsl"....

    <xsl:stylesheet version="1.0"
    xmlnssl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
    <xsl:for-each select="/rss/channel/item"><h2><b>
    <xsl:value-of select="title" /><br /></b></h2>
    <xsl:value-of select="description" /><br /><i>
    Published: <xsl:value-of select="pubDate" /><br /></i>
    <a>
    <xsl:attribute name="href">
    <xsl:value-of select="link"/>
    </xsl:attribute>
    View Story
    </a><br/><br/>
    </xsl:for-each>
    </xsl:template>

    </xsl:stylesheet>

  2. #2
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    You can restrict the number of items to be displayed just by using an if condition in the xsl:for-each loop.

    <xsl:if test="position() &gt; 1">
    ...
    </xsl:if>

    here is your code modified to display only the first 5 items in a feed.

    Code:
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    
    <xsl:template match="/">
    <xsl:for-each select="/rss/channel/item"><h2><b>
       <xsl:if test="position() &gt; 6">
    <xsl:value-of select="title" /><br /></b></h2>
    <xsl:value-of select="description" /><br /><i>
    Published: <xsl:value-of select="pubDate" /><br /></i>
    <a>
    <xsl:attribute name="href">
    <xsl:value-of select="link"/>
    </xsl:attribute>
    View Story
    </a><br/><br/>
       </xsl:if>
    </xsl:for-each>
    </xsl:template>
    
    </xsl:stylesheet>

    Hope this helps… let me now if u have any problem…


    Akram Quraishi is the co-founder of http://www.rapidfeeds.com - A Free online service, which helps anyone, create, edit, publish and track RSS feeds. Sign Up for your FREE account and learn how to Create and Publish your own RSS Feeds today!

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    Kentucky USA
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Changed the style sheet to include your two comments of code with the ...
    <xsl:if test="position() &gt; 6">
    and the closing </xsl:if> statement as stated.
    It now returns an error....
    msxml3.dll error '80004005'
    The stylesheet does not contain a document element. The stylesheet may be empty, or it may not be a well-formed XML document.
    relating to this line of the asp code...
    theOutput = getPage.responseXML.transformNode(xsl)
    Any ideas anyone ????
    Thought it was working .... but it was not ....sorry for the confusion !!!!
    Thanking you in advance.
    Last edited by salmon66; Apr 8, 2005 at 11:02.

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    Kentucky USA
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not working ???

  5. #5
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi,

    i think the error u are getting is not because of the code i have shown u..

    but still try this code..

    let me know if u are have the same error again..

    Bye

    Code:
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    
    <xsl:template match="/">
    <xsl:for-each select="/rss/channel/item">
       <xsl:if test="position() &gt; 6">
    <h2><b>
    <xsl:value-of select="title" /><br /></b></h2>
    <xsl:value-of select="description" /><br /><i>
    Published: <xsl:value-of select="pubDate" /><br /></i>
    <a>
    <xsl:attribute name="href">
    <xsl:value-of select="link"/>
    </xsl:attribute>
    View Story
    </a><br/><br/>
       </xsl:if>
    </xsl:for-each>
    </xsl:template>
    
    </xsl:stylesheet>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •