Re: to extract the longest string (fwd)
Rajagopal, I love academic exercises ;) Actually this didn't turn out to be as messy as I thought it was going to be... >Suppose the align attribute contains strings with more than one characters >How do you find out the longest string? Unless you're happy using a node-set extension function, then you have to use recursion. We're going to recursively go through the row elements, starting with the first one, and using a mode to indicate the type of information we want about the row: <xsl:template match="table"> <xsl:variable name="longest"> <xsl:apply-templates select="row" mode="get-longest" /> </xsl:variable> </xsl:template> Now the recursive template. It matches on a row and then sets two variables: - $current gives the concatenated string of the align attributes on the child cols - $longest gives the result of applying this template on the next row in the table (which in fact gives the longest result from all the rest of the rows) If $longest is longer than $current, then we want to return the $longest string; if $current is longer than $longest, then we want to return the $current string. So in effect calling this template on the second row element returns the longest result from all the rest of the rows, which is then compared to the length of the result from the current row, giving the longest out of those two. <xsl:template match="row" mode="get-longest"> <xsl:variable name="current"> <xsl:for-each select="col"> <xsl:value-of select="@align" /> </xsl:for-each> </xsl:variable> <xsl:variable name="longest"> <xsl:apply-templates select="following-sibling::row" mode="get-longest" /> </xsl:variable> <xsl:choose> <xsl:when test="string-length($longest) > string-length($current)"> <xsl:value-of select="$longest" /> </xsl:when> <xsl:otherwise><xsl:value-of select="$current" /></xsl:otherwise> </xsl:choose> </xsl:template> I've tested this in Xalan, SAXON and MSXML3 (July) and it works in all three. So there you go :) Jeni XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
PURCHASE STYLUS STUDIO ONLINE TODAY!
Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!
Download The World's Best XML IDE!
Accelerate XML development with our award-winning XML IDE - Download a free trial today!
Subscribe in XML format