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

Michael Wechner michael.wechner at wyona.com
Thu Jan 14 15:54:01 CET 2010


Dear Guillaume

That's great stuff. I have added the substring-before-last template to

src/build/yanel2realms.xsl

and also made a note/comment that I have re-used the one from

http://xsltsl.sourceforge.net/

Maybe if we re-use these templates more often we can move it a more 
central place as you describe below.

Btw, the reason I have changed this code is that one can now also 
reference within conf/local/local.realms.xml something like

<realm ...><config src="/home/michi/my-realm/realm-v2.xml"/>

instead only

<realm ...><config src="/home/michi/my-realm/"/> (and one has to 
overwrite realm.xml with realm-v2.xml)

Well, one was also able to do this before, but the realm specific code 
was not compiled and hence could lead to class not found exceptions.

Thanks

Michi




Guillaume Déflache wrote:
> 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