[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Just the first 'x' elements within a for-each


Subject: Re: [xsl] Just the first 'x' elements within a for-each
From: Simon.Fairey@xxxxxx
Date: Tue, 18 Mar 2003 13:51:50 +0000

Jeni,

The following displays all the headlines rather than just the first 2:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
    <xsl:for-each select="/package/stories/story/properties[position() &lt;= 2]">
      <!-- We only want the first 2 headline -->
      <br/>
      <xsl:value-of select="headline"/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Whereas this does work:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
    <xsl:for-each select="/package/stories/story/properties">
      <!-- We only want the first 2 headline -->
      <xsl:if test="position() &lt;= 2">
        <br/>
        <xsl:value-of select="headline"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Si




Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
18/03/2003 11:10
Please respond to Jeni Tennison

 
        To:     Simon.Fairey@xxxxxx
        cc:     xsl-list@xxxxxxxxxxxxxxxxxxxxxx
        Subject:        Re: [xsl] Just the first 'x' elements within a for-each


Hi Si,

> [Using a predicate] doesn't seem to want to work whereas using:
>
> <xsl:if test="position() &lt;= 2">
>
> within the for-each does? Might this be something to do with the XSL
> parsing in XML Spy 5?

I guess that it could be: if you haven't got MSXML3 installed, then
XML Spy might be using MSXML2, which doesn't support XSLT. But that
would depend on the namespace that you're using in the stylesheet. You
don't describe in what way it "doesn't work", so it's hard to tell.

Can you show a full stylesheet with the code that doesn't work and the
code that does?

> Oh and I had a good look around but couldn't find a simple
> description for 'declarative programming'?

I think of it in terms of programming by saying *what* you want to do
rather than in terms of *how* you want to do it. So instead of
phrasing your requirement in procedural terms as "I want to loop over
all the nodes and stop after the second", say "I want to process the
first two nodes".

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/






**********************************************************************************
This email may contain confidential material. If you were not an
intended recipient, please notify the sender and delete all copies.
We may monitor email to and from our network.


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread
Keywords