Jump to content

XSLT - for-each loop - Only output first item found


BryanHood

Recommended Posts

I have the following list:

<Item>  <ItemType>TOOL</ItemType>  <Description>Hammer</Description></Item><Item>  <ItemType>BOOK</ItemType>  <Description>Programming in XSLT</Description></Item><Item>  <ItemType>UTENSIL</ItemType>  <Description>Fork</Description></Item><Item>  <ItemType>BOOK</ItemType>  <Description>Useful Internet Sites</Description></Item>

In the list I have <ItemType>'s of TOOL, BOOK, and UTENSIL.

 

I want to loop through all of the items to find the first one that meets my criteria (in my case BOOK), and output that one only.

 

This code outputs all items that meet the criteria of ItemType's of BOOK.

<xsl:for-each select="ItemType">  <xsl:if test="ItemType = 'BOOK'">    <First_Book_Title>      <xsl:value-of select="Description"/>    </First_Book_Title>  </xsl:if></xsl:for-each>

I'd get this list:

<First_Book_Title>Programming in XSLT</First_Book_Title><First_Book_Title>Useful Internet Sites</First_Book_Title>

I only want to get the book titled "Programming in XSLT".

 

Thank you.

 

 

 

Link to comment
Share on other sites

<xsl:for-each select="Item[ItemType='BOOK']">  <xsl:if test="position()=1">    <First_Book_Title>      <xsl:value-of select="Description"/>    </First_Book_Title>  </xsl:if></xsl:for-each>

The trick was to search for all <Item> nodes that contain the ItemType of "BOOK". select="Item[itemType]='BOOK'] This will give you a list of Item nodes.

 

From the returned list, we only want the first item, so we need to use the position() function.

 

And there you have it! Simple and neat. (Although it did take a while to figure out :( )

Link to comment
Share on other sites

<xsl:for-each select="Item[ItemType='BOOK']">  <xsl:if test="position()=1">    <First_Book_Title>      <xsl:value-of select="Description"/>    </First_Book_Title>  </xsl:if></xsl:for-each>
The trick was to search for all <Item> nodes that contain the ItemType of "BOOK". select="Item[itemType]='BOOK'] This will give you a list of Item nodes. From the returned list, we only want the first item, so we need to use the position() function. And there you have it! Simple and neat. (Although it did take a while to figure out :( )

 

No :( face, :D. Puzzle solving is what programming is about... a least to me. rubes cube we stare at all day while we turn it in our head to find out what side go with what. I have stare at a screen thinking through with the knowledge my novice mind contain from studying to figure out what's wrong with a simple code, until I saw it was missing a ; or typo. You must consume yourself in the life of programming! Remember, the power is yours!(Captain Planet).

Edited by L8V2L
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...