XSLT: how to rebuild a string separated by a delimiter

badtz7229
badtz7229 used Ask the Experts™
on
I have the following string R/0R4/TEST/TEST AVAILABLE/R/12345678901234C1
I want to remove 1 digit from the string after the last "/" such that 1234567890123C1
I then want to rebuild the original string with the newValue such that: R/0R4/TEST/TEST AVAILABLE/R/1234567890123C1

I have the following below which shortens the last string by 1 digit. But I do not know how to rebuild it now.
<xsl:variable name="Num14">
  <xsl:call-template name="substring-after-last">
	<xsl:with-param name="input" select="$elem/Text" />
	<xsl:with-param name="marker" select="'/'" />
  </xsl:call-template>
</xsl:variable>

<xsl:variable name="Cpn" select="substring ( $Num14, 15, 2)"/>
<xsl:variable name="Num13" select="concat ( substring ( $Num14, 1, 13), $Cpn) "/>

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

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Information Architect
Top Expert 2006
Commented:
You could simply reconstruct whilst you are at it

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="1.0">
    
    <xsl:variable name="input-str">R/0R4/TEST/TEST AVAILABLE/R/12345678901234C1</xsl:variable>
    
    <xsl:template match="/">
        <xsl:call-template name="process-last-component">
            <xsl:with-param name="input" select="$input-str" />
            <xsl:with-param name="marker" select="'/'" />
        </xsl:call-template>
    </xsl:template>
    
    <xsl:template name="process-last-component">
        <xsl:param name="input" select="''" />
        <xsl:param name="marker" select="'#'" /><!-- it is dangerous to make the default marker empty -->
        <xsl:choose>
            <xsl:when test="contains($input, $marker)">
                <xsl:value-of select="substring-before($input, $marker)"/>
                <xsl:value-of select="$marker"/>
                <xsl:call-template name="process-last-component">
                    <xsl:with-param name="input" select="substring-after($input, $marker)" />
                    <xsl:with-param name="marker" select="'/'" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="substring ( $input, 1, 13)"/>
                <xsl:value-of select="substring ( $input, 15)"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
 
    
    
</xsl:stylesheet>

Open in new window

Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
simply put back what you have just taken away
only do the processing on the last component before you put it back

Author

Commented:
thank you. that worked.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial