Re: [xsl] String manipulation XSLT 1.0

Subject: Re: [xsl] String manipulation XSLT 1.0
From: Emmanuel Bégué <eb@xxxxxxxxxx>
Date: Fri, 24 Sep 2010 13:41:21 +0200
What about:

<xsl:template match="given-name">
	<xsl:variable name="replaced">
		<xsl:call-template name="replace-substring">
			<xsl:with-param name="original" select="."/>
			<xsl:with-param name="substring" select="'.'"/>
			<xsl:with-param name="replacement" select="'. '"/>
			</xsl:call-template>
		</xsl:variable>
	<xsl:copy>
		<xsl:value-of select="normalize-space($replaced)"/>
		</xsl:copy>
	</xsl:template>

(no change to "replace-substring")

Input:
<given-name>A.H.</given-name>
<given-name>A. H.O.</given-name>
<given-name>X-Y.Z.B.</given-name>

output:
<given-name>A. H.</given-name>
<given-name>A. H. O.</given-name>
<given-name>X-Y. Z. B.</given-name>

Is this correct?

Regards,
EB


On Fri, Sep 24, 2010 at 12:56 PM,  <pankaj.c@xxxxxxxxxxxxxxxxxx> wrote:
>>What do you mean "all spaces"? It only removes leading and trailing
>>spaces, and duplicate spaces, but it will leave a space between two
>>non-space characters if there was at least one...
>
> If you read my original post or whole thread it says
>
> I have an element,
>
> Input
> ====
> <given-name>A.H.</given-name>
>
> Output
> ====
> <given-name>A. H.</given-name> <!--  Notice the space after A.  -->
>
> Martin has given a nice solution for it and almost fits the bill except I
> do not need space after "." in last initial.
>
> The best I have done is below. Only the issue with <xsl:template
> name="replace-substring"> which adds space even after the last initial. So
> if I use normalize-space() it removes the extra space added by template or
> MIGHT POSSIBLE I AM NOT CORRECTLY USING IT.
> Hope this makes myself clear.
>
>
>
> <xsl:template match="given-name">
> <xsl:copy>
>    <xsl:choose>
>    <xsl:when test="string-length(.)=2 and contains(.,'.')">
>        <xsl:value-of select="."/>
>    </xsl:when>
>    <xsl:when test="string-length(.)=4 and contains(.,'.')">
>        <xsl:value-of select="concat(substring-before(., '.'), '. ',
> substring-after(., '.'))"/>    </xsl:when>
>    <xsl:otherwise>
>
>                    <xsl:call-template name="replace-substring">
>                        <xsl:with-param name="original" select="."/>
>                        <xsl:with-param name="substring" select="'.'"/>
>                        <xsl:with-param name="replacement" select="'. '"/>
>                    </xsl:call-template>
>    </xsl:otherwise>
>    </xsl:choose>
>  </xsl:copy>
> </xsl:template>
>
> <xsl:template name="replace-substring">
> <xsl:param name="original"/>
> <xsl:param name="substring"/>
> <xsl:param name="replacement" select="'. '"/>
> <xsl:choose>
>    <xsl:when test="contains($original, $substring)">
>        <xsl:value-of select="substring-before($original, $substring)"/>
>        <xsl:copy-of select="$replacement"/>
>        <xsl:call-template name="replace-substring">
>            <xsl:with-param name="original"
> select="substring-after($original, $substring)"/>
>            <xsl:with-param name="substring" select="$substring"/>
>            <xsl:with-param name="replacement" select="$replacement"/>
>        </xsl:call-template>
>    </xsl:when>
>    <xsl:otherwise>
>        <xsl:value-of select="$original"/>
>    </xsl:otherwise>
> </xsl:choose>
> </xsl:template>

Current Thread