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

M

martin.tschofen

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
 
V

Volkm@r

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
 
D

Dimitre Novatchev

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top