0

I have a string that looks like this: some, , , , text
I need it to look like this: some, text
There is unknown number of such commas, it may be 2, it may be 10
I tried recursive approach but it did not work as expected:

<xsl:template name="remove-duplicate">
        <xsl:param name="text" />
        <xsl:param name="remove" />

        <xsl:value-of select="$text" />
        <xsl:if test="contains($text, $remove)">
            <xsl:call-template name="string-replace-all">
                <xsl:with-param name="text" select="$text" />
                <xsl:with-param name="replace" select="$remove" />
                <xsl:with-param name="by" select="''" />
            </xsl:call-template>
            <xsl:call-template name="remove-duplicate">
                <xsl:with-param name="text" select="$text" />
                <xsl:with-param name="remove" select="$remove" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

Calling the template:

    <xsl:call-template name="remove-duplicate">
        <xsl:with-param name="text" select="string(.)" />
        <xsl:with-param name="remove" select="' ,'" />
    </xsl:call-template>

The string-replace-all was stolen from this question: XSLT string replace

Audoryosa
  • 19
  • 5

1 Answers1

0

How about:

<xsl:value-of select="translate(normalize-space(translate($yourString, ', ', ' &#133;')), ' &#133;', ', ')"/>

Note that this assumes your string does not contain any whitespace characters other than spaces.

If this assumption is not true, try the solution posted here: https://stackoverflow.com/a/38177946/3016153

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51