[Yanel-dev] handy XSLT functions wanted: ends-with, substring-before-last, substring-after-last

Guillaume Déflache guillaume.deflache at wyona.com
Thu Jan 14 12:42:11 CET 2010


Hi!

The handy functions customarily named "ends-with", 
"substring-before-last" and "substring-after-last" are unfortunately not 
available by default in XSLT 1 or EXSLT (they might in XSLT 2) AFAIK.

It would be nice to have them available in Yanel, because they are often 
useful when manipulating paths.

I suggest we had them under src/webapp/htdocs/commons/ using the name of 
the functions as filenames (i.e. one function per file).


And now my random notes about them:

*substring-before-last*, *substring-after-last*:
http://www.google.com/search?q=XSLT+%22substring-before-last%22
- [LGPL, XSLT 2.0] 
http://www.xsltfunctions.com/xsl/functx_substring-before-last.html
- [LGPL (see ANNOUNCE in archive), both XSLT 1.0] 
http://xsltsl.sourceforge.net/string.html#template.str:substring-before-last
- EXSLT http://www.exslt.org/regexp/functions/replace/ 
http://www.exslt.org/str/functions/split/
- Java extension + 
http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html#sum
- [license: small excerpts are OK: 'We appreciate, but do not require, 
attribution. An attribution usually includes the title, author, 
publisher, and ISBN. For example: "XSLT Cookbook, Second Edition by Sal 
Mangano. Copyright 2006 O'Reilly Media, Inc., 0-596-00974-7."' both XSLT 
1.0, in "XSLT Cookbook, 2nd Edition"] 
http://my.safaribooksonline.com/0596009747/xsltckbk2-CHP-2-SECT-4
 > <xsl:template name="str: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="str: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>
- `basename` named template from James Clark 
http://www.stylusstudio.com/xsllist/199905/post60130.html

*ends-with*:
substring($value, (string-length($value) - string-length($substr)) + 1) 
= $substr http://my.safaribooksonline.com/0596009747/xsltckbk2-CHP-2-SECT-1


HTH,
    Guillaume


More information about the Yanel-development mailing list