jjasper0729
jjasper0729

Reputation: 295

XSL 1.0 How to replace two different things in a string

I've got a string that could have line breaks and apostrophes. I need to do a replace for both. I have the following xsl code:

<xsl:call-template name="replaceapostrophes">
<xsl:with-param name="string">
    <xsl:call-template name="replacelinefeeds">
        <xsl:with-param name="string" select="hl7:text/hl7:paragraph"/>
    </xsl:call-template>
</xsl:with-param>
</xsl:call-template>

    <!-- template for replacing line feeds with <br> tags for page display -->
<xsl:template name="replacelinefeeds">
    <xsl:param name="string"/>
    <xsl:choose>
        <xsl:when test="contains($string,'&#10;')">
            <xsl:value-of select="substring-before($string,'&#10;')"/>
            <br/>
            <xsl:call-template name="replacelinefeeds">
                <xsl:with-param name="string" select="substring-after($string,'&#10;')"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$string"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<!-- template for replacing html encoded apostrophes for page display -->
<xsl:template name="replaceapostrophes">
    <xsl:param name="string"/>
    <xsl:choose>
        <xsl:when test="contains($string, '&amp;#39;')">
            <xsl:value-of select="substring-before($string,'&amp;#39;')"/>'<xsl:call-template name="replaceapostrophes">
                <xsl:with-param name="string" select="substring-after($string,'&amp;#39;')"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$string"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

This is the xml code:

<text>
  <paragraph>Adding apostrophe to the patient&amp;#39;s instructions
  and checking for a second line</paragraph>
</text>

However, when this runs, I'm getting the apostrophe accounted for, but not the line breaks.

Adding apostrophe to the patient's instructions and checking for a second line

rather than

Adding apostrophe to the patient's instructions
and checking for a second line

It does work fine if there's one or the other but not both in the same string.

Is there a different way I need to do these?

Thanks

Upvotes: 0

Views: 134

Answers (2)

samjudson
samjudson

Reputation: 56893

Try doing it the other way around (replace apostrophes first, then line breaks).

Basically you are putting an HTML <br/> element into your variable, and then taking its text value to replace the apostrophes , which removes the line break again.

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167716

Use the templates the other way round, i.e. first replace the apostrophes, then the line feeds. And make sure for the result of the replacement of line feeds you use xsl:copy-of and not xsl:value-of when you output it, as otherwise the br elements will be lost. So in case you have <xsl:variable name="text"><xsl:call-template name="replacelinefeeds">..</xsl:call-template></xsl:variable>, make sure you use <xsl:copy-of select="$text"/>.

Upvotes: 1

Related Questions