BryanHood Posted April 25, 2014 Share Posted April 25, 2014 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 More sharing options...
L8V2L Posted April 27, 2014 Share Posted April 27, 2014 What made you learn xml. Link to comment Share on other sites More sharing options...
BryanHood Posted April 29, 2014 Author Share Posted April 29, 2014 L8V2L, At my work we use XML and XSLT translations to deliver information to various 3rd party software packages. We create our own "generic" data dumps, and then use XSLT to transform it into whatever is needed for them. Link to comment Share on other sites More sharing options...
BryanHood Posted April 29, 2014 Author Share Posted April 29, 2014 <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 More sharing options...
L8V2L Posted April 29, 2014 Share Posted April 29, 2014 (edited) <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, . 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 April 29, 2014 by L8V2L Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now