XML For Loop Query

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Iain

    #1

    XML For Loop Query

    Hello. I'm new to the XML lark, I've got a script to display some RSS feeds
    on my ASP pages, but I can't work out how to just display the first three...

    My XSL stylesheet looks like this... can anyone give me a tip as to how to
    stop the for loop after 3 records?

    TIA. :)

    <?xml version="1.0" ?>
    <xsl:styleshe et version="1.0"
    xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="ye s" indent="yes"/>
    <xsl:template match="*">

    <xsl:for-each select="//*[local-name()='item']">
    <a>
    <xsl:attribut e name="href">
    <xsl:value-of select="*[local-name()='link']"/>
    </xsl:attribute>
    <xsl:attribut e name="target">
    <xsl:text>top </xsl:text>
    </xsl:attribute>
    <xsl:value-of select="*[local-name()='title']"/>
    </a><br/>
    <xsl:value-of select="*[local-name()='descrip tion']"
    disable-output-escaping="yes"/>
    <br /><br/>
    </xsl:for-each>

    </xsl:template>
    <xsl:template match="/">
    <xsl:apply-templates/>
    </xsl:template>
    </xsl:stylesheet>


  • Richard Tobin

    #2
    Re: XML For Loop Query

    In article <A8GdnRlEd6DfhM rcRVn-jA@eclipse.net. uk>,
    Iain <iain.g@lycos.c o.you-kay> wrote:
    [color=blue]
    >Hello. I'm new to the XML lark, I've got a script to display some RSS feeds
    >on my ASP pages, but I can't work out how to just display the first three...
    >
    >My XSL stylesheet looks like this... can anyone give me a tip as to how to
    >stop the for loop after 3 records?[/color]
    [color=blue]
    ><xsl:for-each select="//*[local-name()='item']">[/color]

    <xsl:for-each select="(//*[local-name()='item'])[position() <= 3]">

    -- Richard

    Comment

    • Iain

      #3
      Re: XML For Loop Query



      "Richard Tobin" <richard@cogsci .ed.ac.uk> wrote in message
      news:<cj773n$n7 1$1@pc-news.cogsci.ed. ac.uk>...
      [color=blue]
      > In article <A8GdnRlEd6DfhM rcRVn-jA@eclipse.net. uk>,[/color]
      [color=blue]
      > Iain <iain.g@lycos.c o.you-kay> wrote:[/color]
      [color=blue]
      >[/color]
      [color=blue][color=green]
      > >Hello. I'm new to the XML lark, I've got a script to display some RSS[/color][/color]
      feeds
      [color=blue][color=green]
      > >on my ASP pages, but I can't work out how to just display the first[/color][/color]
      three...
      [color=blue][color=green]
      > >[/color][/color]
      [color=blue][color=green]
      > >My XSL stylesheet looks like this... can anyone give me a tip as to how[/color][/color]
      to
      [color=blue][color=green]
      > >stop the for loop after 3 records?[/color][/color]
      [color=blue]
      >[/color]
      [color=blue][color=green]
      > ><xsl:for-each select="//*[local-name()='item']">[/color][/color]
      [color=blue]
      >[/color]
      [color=blue]
      > <xsl:for-each select="(//*[local-name()='item'])[position() <= 3]">[/color]
      [color=blue]
      >[/color]
      [color=blue]
      > -- Richard[/color]
      [color=blue]
      >[/color]



      Apologies if I emailed you twice with this, hit the wrong button...

      That gives me:

      The stylesheet does not contain a document element. The stylesheet may be
      empty, or it may not be a well-formed XML document.

      ASP function calling it looks like this, although it works fine when writing
      out every record in the feed:

      Function getXML(sourceFi le)

      dim styleFile

      dim source, style

      styleFile = Server.MapPath( "style/news.xsl")

      Dim xmlhttp

      Set xmlhttp = Server.CreateOb ject("Microsoft .XMLHTTP")

      xmlhttp.Open "GET", sourceFile, false

      xmlhttp.Send

      set source = Server.CreateOb ject("Microsoft .XMLDOM")

      source.async = false

      source.loadxml( xmlhttp.Respons eText)

      set style = Server.CreateOb ject("Microsoft .XMLDOM")

      style.async = false

      style.load(styl eFile)

      getXML = source.transfor mNode(style)

      set source = nothing

      set style = nothing

      End Function


      Comment

      • Richard Tobin

        #4
        Re: XML For Loop Query

        In article <19udnU_iqrIIsc rcRVn-uQ@eclipse.net. uk>,
        Iain <iain.g@lycos.c o.you-kay> wrote:
        [color=blue]
        >That gives me:
        >
        >The stylesheet does not contain a document element. The stylesheet may be
        >empty, or it may not be a well-formed XML document.[/color]

        (Not the greatest error message!)

        Sorry, forgot to escape the less-than:
        [color=blue][color=green]
        > > <xsl:for-each select="(//*[local-name()='item'])[position() <= 3]">[/color][/color]

        should be

        <xsl:for-each select="(//*[local-name()='item'])[position() &lt;= 3]">

        -- Richard

        Comment

        • Patrick TJ McPhee

          #5
          Re: XML For Loop Query

          In article <cj7njt$s8c$1@p c-news.cogsci.ed. ac.uk>,
          Richard Tobin <richard@cogsci .ed.ac.uk> wrote:

          % should be
          %
          % <xsl:for-each select="(//*[local-name()='item'])[position() &lt;= 3]">

          I always try to use > in xpath expressions to avoid this issue.

          <xsl:for-each select="(//*[local-name()='item'])[3 >= position()]">
          --

          Patrick TJ McPhee
          East York Canada
          ptjm@interlog.c om

          Comment

          Working...