I. Using a recursively named named template :
This conversion is :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:call-template name="eatAllSlashes"> <xsl:with-param name="pText" select="."/> </xsl:call-template> </xsl:template> <xsl:template name="eatAllSlashes"> <xsl:param name="pText"/> <xsl:choose> <xsl:when test="not(contains($pText,'/'))"> <xsl:value-of select="$pText"/> </xsl:when> <xsl:otherwise> <xsl:call-template name="eatAllSlashes"> <xsl:with-param name="pText" select="substring-after($pText, '/')"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
when applied to this XML document :
<t>http:
creates the desired correct output :
is-it-possible-to-slice-the-end-of-a-url-with-xslt-1-0
II. Using FXSL library :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="my:my" exclude-result-prefixes="xsl my"> <xsl:import href="iter.xsl"/> <xsl:output method="text"/> <my:condition/> <my:skipSlash/> <xsl:variable name="vfunCondition" select="document('')/*/my:condition"/> <xsl:variable name="vfunSkipSlash" select="document('')/*/my:skipSlash"/> <xsl:template match="/"> <xsl:call-template name="iterUntil"> <xsl:with-param name="pCond" select="$vfunCondition"/> <xsl:with-param name="pFun" select="$vfunSkipSlash"/> <xsl:with-param name="arg1" select="string(/)"/> </xsl:call-template> </xsl:template> <xsl:template match="my:condition"> <xsl:param name="arg1"/> <xsl:value-of select="number(not(contains($arg1, '/')))"/> </xsl:template> <xsl:template match="my:skipSlash"> <xsl:param name="arg1"/> <xsl:value-of select="substring-after($arg1, '/')"/> </xsl:template> </xsl:stylesheet>
When this conversion is applied to this XML document:
<t>http:
The desired result is obtained :
is-it-possible-to-slice-the-end-of-a-url-with-xslt-1-0
Note:
The iterUntil template has three parameters:
- pCond - a function (link to a template) that checks the condition for the current result and potentially generates a “stop signal” (1).
- pFun is a function (link to the template), which is used to create the next current result from the current result (or initially from the input argument $ arg1).
- arg1 is the input argument from which the pFun function was originally applied.
Our pCond function is a template that matches my:condition . It gives a “stop signal” (outputs 1) only if the line passed as $arg1 does not contain the characters “/”.
Our pFun function is a template that matches my:skipSlash . It discards all characters before and includes the first string '/' in the string $arg1
The initial input argument is defined in $arg1 and is a string value from which only text after the last '/' should be created.
The main advantage of using FXSL is that it avoids the need for coding explicit recursion and the potential for errors. In addition, the template / functions are very versatile and powerful and can be reused to solve huge classes of similar problems.
Dimitre novatchev
source share