Is it possible to cut off the end of a url using XSLT 1.0?

Using only the string functions of XSLT 1.0, how could I cut off the end of a URL?

So from

http://stackoverflow.com/questions/2981175/is-it-possible-to-slice-the-end-of-a-url-with-xslt-1-0

I would like to extract

it's possible to a slice-of-the-end-of-a-url-s-1-0-xslt

Is it possible?

+7
string xslt
source share
2 answers

Unfortunately, XSLT / XPath 1.0 does not have a substring-after-last function. So, to get the last part of the URL, you have to write a recursive pattern as explained by Jeni Tenisson :

<xsl:template name="substring-after-last"> <xsl:param name="string" /> <xsl:param name="delimiter" /> <xsl:choose> <xsl:when test="contains($string, $delimiter)"> <xsl:call-template name="substring-after-last"> <xsl:with-param name="string" select="substring-after($string, $delimiter)" /> <xsl:with-param name="delimiter" select="$delimiter" /> </xsl:call-template> </xsl:when> <xsl:otherwise><xsl:value-of select="$string" /></xsl:otherwise> </xsl:choose> </xsl:template> 

This template will be called, for example. eg:

 <xsl:call-template name="substring-after-last"> <xsl:with-param name="string" select="$url" /> <xsl:with-param name="delimiter" select="'/'" /> </xsl:call-template> 
+7
source share

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://stackoverflow.com/questions/2981175/is-it-possible-to-slice-the-end-of-a-url-with-xslt-1-0</t> 

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://stackoverflow.com/questions/2981175/is-it-possible-to-slice-the-end-of-a-url-with-xslt-1-0</t> 

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.

+2
source share

All Articles