This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: text retrieval problem
| | How do I get the last number in a string like:
| | "[sdsdf] 234 sdf 50"
If you are a Saxon 5.4 user, you can also play with a
neat extension that Mike Kay has added recently for
defining extension functions using XSLT itself
as the language for the function's implementation.
I downloaded Saxon 5.4 recently and was playing with this
(of course proprietary, yet very interesting) addition.
Here's an example of the stylesheet from my last
posting that uses the standard <xsl:call-template>
approach for non-Saxon processors and instead uses
an XSLT-based extension function if running
Saxon 5.4 To see the difference, I made the
function return the last number in the string *plus one*.
I'm assuming there's not a performance difference
between the two, but the difference is the syntax
of invoking the functionality:
<xsl:value-of select="util:last-num-plus-one(.)"/>
versus the standard:
<xsl:call-template name="last-number">
<xsl:with-param name="str" select="."/>
</xsl:call-template>
Assuming your input document is again:
<List>
<Part>[sdsdf] 234 sdf 50</Part>
<Part>1123</Part>
<Part>ABC 123</Part>
</List>
Running the transformation with a non-Saxon XSLT engine
produces:
<result>
<LastNum>50</LastNum>
<LastNum>1123</LastNum>
<LastNum>123</LastNum>
</result>
And with Saxon (remember the function's intentionally
adding one to the result!)...
<result>
<LastNum>51</LastNum>
<LastNum>1124</LastNum>
<LastNum>124</LastNum>
</result>
The stylesheet looks like:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:saxon="http://icl.com/saxon"
xmlns:util="urn:something"
extension-element-prefixes="saxon"
exclude-result-prefixes="saxon util">
<xsl:output indent="yes"/>
<xsl:template match="/List">
<result>
<xsl:for-each select="Part">
<LastNum>
<xsl:choose>
<!-- If extension function is available, use it -->
<xsl:when test="function-available('util:last-num-plus-one')">
<xsl:value-of select="util:last-num-plus-one(.)"/>
</xsl:when>
<!-- Otherwise, go the call-template route -->
<xsl:otherwise>
<xsl:call-template name="last-number">
<xsl:with-param name="str" select="."/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</LastNum>
</xsl:for-each>
</result>
</xsl:template>
<!-- Example using recursive named template -->
<xsl:template name="last-number">
<xsl:param name="str"/>
<xsl:variable name="left" select="substring-after($str,' ')"/>
<xsl:choose>
<xsl:when test="$left=''">
<xsl:value-of select="number($str)"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="last-number">
<xsl:with-param name="str" select="$left"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!--
| Example using recursive saxon XSLT extension function
| Requires Saxon 5.4 or greater.
+-->
<saxon:function name="util:last-num-plus-one">
<xsl:param name="str"/>
<xsl:variable name="left" select="substring-after($str,' ')"/>
<xsl:choose>
<xsl:when test="$left=''">
<saxon:return select="number($str)+1"/>
</xsl:when>
<xsl:otherwise>
<saxon:return select="util:last-num-plus-one($left)"/>
</xsl:otherwise>
</xsl:choose>
</saxon:function>
</xsl:stylesheet>
Have fun.
______________________________________________________________
Steve Muench, Lead XML Evangelist & Consulting Product Manager
BC4J & XSQL Servlet Development Teams, Oracle Rep to XSL WG
Author "Building Oracle XML Applications", O'Reilly
http://www.oreilly.com/catalog/orxmlapp/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list