Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Regular expression"-style replace in XSLT 1.0

Tags:

xslt

xslt-1.0

I need to perform a find and replace using XSLT 1.0 which is really suited to regular expressions. Unfortunately these aren't available in 1.0 and I'm also unable to use any extension libraries such as EXSLT due to security settings I can't change.

The string I'm working with looks like:

19;#John Smith;#17;#Ben Reynolds;#1;#Terry Jackson

I need to replace the numbers and ; # characters with a ,. For example the above would change to:

John Smith, Ben Reynolds, Terry Jackson

I know a recursive string function is required, probably using substring and translate, but I'm not sure where to start with it.

Does anyone have some pointers on how to work this out? Here's what I've started with:

<xsl:template name="TrimMulti">
    <xsl:param name="FullString" />
    <xsl:variable name="NormalizedString">
        <xsl:value-of select="normalize-space($FullString)" />
    </xsl:variable>
    <xsl:variable name="Hash">#</xsl:variable>
    <xsl:choose>
        <xsl:when test="contains($NormalizedString, $Hash)">
            <!-- Do something and call TrimMulti -->
        </xsl:when>
    </xsl:choose>
</xsl:template>
like image 448
Alex Angas Avatar asked Jun 25 '09 13:06

Alex Angas


People also ask

How to use replace in XSLT?

XSLT replace is deterministic and does string manipulation that replaces a sequence of characters defined inside a string that matches an expression. In simple terms, it does string substitution in the specified place by replacing any substrings. Fn: replace function is not available in XSLT1.

What is regex in XSLT?

The regex-group() function names which matched string you want to use inside of the xsl:matching-substring element; pass it a 1 to get the first, a 2 to get the second, and so forth. The example above uses it to plug the three matched values inside new city, state, and zip elements created for the output.


1 Answers

I'm hoping you haven't simplified the problem too much for asking it on SO, because this shouldn't be that much of a problem.

You can define a template and recursively call it as long as you keep the input string's format consistent.

For example,

<xsl:template name="TrimMulti">
  <xsl:param name="InputString"/>
  <xsl:variable name="RemainingString" 
    select="substring-after($InputString,';#')"/>
  <xsl:choose>
    <xsl:when test="contains($RemainingString,';#')">
      <xsl:value-of 
        select="substring-before($RemainingString,';#')"/>
      <xsl:text>, </xsl:text>
      <xsl:call-template name="TrimMulti">
        <xsl:with-param 
          name="InputString"
          select="substring-after($RemainingString,';#')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$RemainingString"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

I tested this template out with the following call:

<xsl:template match="/">
  <xsl:call-template name="TrimMulti">
    <xsl:with-param name="InputString">19;#John Smith;#17;#Ben Reynolds;#1;#Terry Jackson</xsl:with-param>
  </xsl:call-template>
</xsl:template>

And got the following output:

John Smith, Ben Reynolds, Terry Jackson

Which seems to be what you're after.

The explanation of what it is doing is easy to explain if you're familiar with functional programming. The InputString parameter is always in the form [number];#[name];#[rest of string]. Each call of the TrimMulti template chops off the [number];# part and prints off the [name] part, then passes the remaining expression to itself recursively.

The base case is when InputString is in the form [number];#[name], in which case the RemainingString variable won't contain ;#. Since we know this is the end of the input, we don't output a comma this time.

like image 145
Welbog Avatar answered Sep 21 '22 07:09

Welbog