xsl-list
[Top] [All Lists]

Re: [xsl] String manipulation XSLT 1.0

2010-09-24 05:56:47
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>

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--