SPFE Documentation | Collections > SPFE Function Reference > index-of-longest-string
Function: index-of-longest-string
index-of-longest-string(strings as xs:string*, current as xs:integer, length-of-longest as xs:integer, index-of-longest as xs:integer) as xs:integer
- Description
-
The index-of-longest-string function returns the index of the longest string in a sequence of strings. To get the index of the shortest string, use index-of-shortest-string.
For XSLT geeks: index-of-longest-string is a recursive polymorphic function. There is a version with one parameter, strings, that is the one you call from your code, and a version with four parameters, that is called by the first version, and which calls itself recursively, to do the work. It is unlikely you will ever want to call the second version yourself.
- Return value
-
Return type: xs:integer
The index of the longest string.
- Source file
-
$SPFEOT_HOME/1.0/scripts/common/utility-functions.xsl
Parameters
- strings
-
Type: xs:string*
A sequence of strings.
- current
-
Type: xs:integer
The index of the string currently being measured. You should not generally use this parameter in your code. It is used internally by the recursive function calls.
- length-of-longest
-
Type: xs:integer
The length of the longest string seen so far as the function is called recursively. You should not generally use this parameter in your code.
- index-of-longest
-
Type: xs:integer
The index of the longest string seen so far as the function is called recursively. You should not generally use this parameter in your code.
Definition
<xsl:function name="sf:index-of-longest-string" as="xs:integer"> <xsl:param name="strings" as="xs:string*"/> <xsl:value-of select="sf:index-of-longest-string($strings,2,string-length($strings[1]),1)"/> </xsl:function>
<xsl:function name="sf:index-of-longest-string" as="xs:integer"> <xsl:param name="strings" as="xs:string*"/> <xsl:param name="current" as="xs:integer"/> <xsl:param name="length-of-longest" as="xs:integer"/> <xsl:param name="index-of-longest" as="xs:integer"/> <xsl:message select="'sf:longest-string',$strings,'|', $current,'|', $length-of-longest, '|', $index-of-longest"/> <xsl:choose > <xsl:when test="$current le count($strings)"> <xsl:variable name="length-of-current" select="string-length($strings[$current])"/> <xsl:value-of select="sf:index-of-longest-string( $strings, $current + 1, max(($length-of-current,$length-of-longest)), if ($length-of-current>$length-of-longest) then $current else $index-of-longest)"/> </xsl:when> <xsl:otherwise > <xsl:value-of select="$index-of-longest"/> </xsl:otherwise> </xsl:choose> </xsl:function>