You want to find the index of a substring within a string rather than the text before or after the substring.
<xsl:template name="index-of"> <xsl:param name="input"/> <xsl:param name="substr"/> <xsl:choose> <xsl:when test="contains($input, $substr)"> <xsl:value-of select="string-length(substring-before($input, $substr))+1"/> </xsl:when> <xsl:otherwise>0</xsl:otherwise> </xsl:choose> </xsl:template>
The position of a substring within another string is simply the
length of the string preceding it plus 1. If you are certain that the
target string contains the substring, then you can simply use
string-length(substring-before($value,
$substr))+1
. However, in general, you need a way
to handle the case in which the substring is not present. Here, zero
is chosen as an indication of this case, but you can use another
value such as -1
or NaN
.