select only the first node from a for-each loop that contains a specific element

Discussion in 'XML' started by martin.tschofen@gmail.com, May 18, 2005.

  1. Guest

    How do I select only the first node from a for-each loop that contains
    a the element "photo".

    the following xsl finds all "art" elements that have a "photo" element.
    The problem is that I only want to output the first found result. The
    way I've written the xsl I get a couple "photo" elements.
    Here's my xsl:

    <xsl:template match="/topixfeed">
    <!--some additional stuff happens here that finds the parent of all
    "art" elements-->
    <xsl:for-each select="art">
    <xsl:choose>
    <xsl:when test='photo'>
    <h3><xsl:number/><xsl:value-of select="title"/></h3>
    </xsl:when>
    </xsl:choose>
    </xsl:for-each>
    </div>
    </xsl:template>

    Thanks for any help...mt
     
    , May 18, 2005
    #1
    1. Advertising

  2. Volkm@r Guest

    Re: select only the first node from a for-each loop that containsa specific element

    wrote:
    > How do I select only the first node from a for-each loop that contains
    > a the element "photo".
    >
    > the following xsl finds all "art" elements that have a "photo" element.
    > The problem is that I only want to output the first found result. The
    > way I've written the xsl I get a couple "photo" elements.
    > Here's my xsl:
    >
    > <xsl:template match="/topixfeed">
    > <!--some additional stuff happens here that finds the parent of all
    > "art" elements-->
    > <xsl:for-each select="art">
    > <xsl:choose>
    > <xsl:when test='photo'>
    > <h3><xsl:number/><xsl:value-of select="title"/></h3>
    > </xsl:when>
    > </xsl:choose>
    > </xsl:for-each>
    > </div>
    > </xsl:template>
    >
    > Thanks for any help...mt
    >

    <http://zvon.org/xxl/XPathTutorial/Output/example4.html#b9b1c33c23>

    So you might try

    <xsl:template match="/topixfeed/art/photo[1]">
    <h3><xsl:number/><xsl:value-of select="title"/></h3>
    </xsl:template>
    <xsl:template match="/topixfeed/art/photo">
    <!--Your action for all the others but the first onee -->
    </xsl:template>

    HTH
    --
    Volkm@r
     
    Volkm@r, May 18, 2005
    #2
    1. Advertising

  3. <> wrote in message
    news:...
    > How do I select only the first node from a for-each loop that contains
    > a the element "photo".
    >
    > the following xsl finds all "art" elements that have a "photo" element.
    > The problem is that I only want to output the first found result. The


    This is an artificially created problem. If only the first element in the
    node-set must be processed, then instead of:

    <xsl:for-each select="$someNode-set">
    <!-- Whatever processing is necessary here -->
    </xsl:for-each>

    Use:
    <xsl:for-each select="($someNode-set")[1]>
    <!-- Whatever processing is necessary here -->
    </xsl:for-each>


    Cheers,
    Dimitre Novatchev

    > way I've written the xsl I get a couple "photo" elements.
    > Here's my xsl:
    >
    > <xsl:template match="/topixfeed">
    > <!--some additional stuff happens here that finds the parent of all
    > "art" elements-->
    > <xsl:for-each select="art">
    > <xsl:choose>
    > <xsl:when test='photo'>
    > <h3><xsl:number/><xsl:value-of select="title"/></h3>
    > </xsl:when>
    > </xsl:choose>
    > </xsl:for-each>
    > </div>
    > </xsl:template>
    >
    > Thanks for any help...mt
    >
     
    Dimitre Novatchev, May 18, 2005
    #3
    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.
Similar Threads
  1. Replies:
    0
    Views:
    1,568
  2. Tjerk Wolterink
    Replies:
    2
    Views:
    1,458
    Dimitre Novatchev
    Aug 24, 2006
  3. William  Krick
    Replies:
    4
    Views:
    14,541
    anntenna
    Mar 18, 2011
  4. Little
    Replies:
    3
    Views:
    1,079
    John B. Matthews
    Aug 17, 2010
  5. Isaac Won
    Replies:
    9
    Views:
    397
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page