December 2002
Intermediate to advanced
672 pages
16h 53m
English
XSLT does not have any functions for searching strings in reverse.
Using recursion, you can emulate a reverse search with a search for
the last occurrence of substr. Using this
technique, you can create a substring-before-last
and a substring-after-last.
<xsl:template name="substring-before-last"> <xsl:param name="input" /> <xsl:param name="substr" /> <xsl:if test="$substr and contains($input, $substr)"> <xsl:variable name="temp" select="substring-after($input, $substr)" /> <xsl:value-of select="substring-before($input, $substr)" /> <xsl:if test="contains($temp, $substr)"> <xsl:value-of select="$substr" /> <xsl:call-template name="substring-before-last"> <xsl:with-param name="input" select="$temp" /> <xsl:with-param name="substr" select="$substr" /> </xsl:call-template> </xsl:if> </xsl:if> </xsl:template> <xsl:template name="substring-after-last"> <xsl:param name="input"/> <xsl:param name="substr"/> <!-- Extract the string which comes after the first occurence --> <xsl:variable name="temp" select="substring-after($input,$substr)"/> <xsl:choose> <!-- If it still contains the search string the recursively process --> <xsl:when test="$substr and contains($temp,$substr)"> <xsl:call-template name="substring-after-last"> <xsl:with-param name="input" select="$temp"/> <xsl:with-param name="substr" select="$substr"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$temp"/> </xsl:otherwise> </xsl:choose> </xsl:template> ...
Read now
Unlock full access