Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to slice off the end of a URL with XSLT 1.0?

Tags:

string

xslt

Using only XSLT 1.0's string functions, how would I go about slicing 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

is-it-possible-to-slice-the-end-of-a-url-with-xslt-1-0

Is this possible?

like image 767
Eric Avatar asked Jun 05 '10 16:06

Eric


People also ask

How do I substring in XSLT?

Substring is primarily used to return a section of a string or truncating a string with the help of the length provided. XSLT is incorporated with XPath while manipulating strings of text when XSLT access. The Substring function takes a string as an argument with the numbers one or two and an optional length.

What is substring after in XSLT?

substring-after() Function — Returns the substring of the first argument after the first occurrence of the second argument in the first argument. If the second argument does not occur in the first argument, the substring-after() function returns an empty string.

Is XSL and XSLT the same?

XSLT is designed to be used as part of XSL. In addition to XSLT, XSL includes an XML vocabulary for specifying formatting. XSL specifies the styling of an XML document by using XSLT to describe how the document is transformed into another XML document that uses the formatting vocabulary.

What is string length in XSLT?

XSLT string length is defined as a string function and takes a single string argument and returns an integer value representing the number of characters in the string. It converts any declared type into a string except that an empty parenthesis cannot be converted. The whitespaces are taken into the count.


1 Answers

Unfortunately there is no substring-after-last function in XSLT/XPath 1.0. So to get the last part of an URL you would have to write a recursive template 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 would be called e.g. like this:

<xsl:call-template name="substring-after-last">
  <xsl:with-param name="string" select="$url" />
  <xsl:with-param name="delimiter" select="'/'" />
</xsl:call-template>
like image 180
Dirk Vollmar Avatar answered Nov 15 '22 09:11

Dirk Vollmar