Reputation: 295
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,' ')">
<xsl:value-of select="substring-before($string,' ')"/>
<br/>
<xsl:call-template name="replacelinefeeds">
<xsl:with-param name="string" select="substring-after($string,' ')"/>
</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, '&#39;')">
<xsl:value-of select="substring-before($string,'&#39;')"/>'<xsl:call-template name="replaceapostrophes">
<xsl:with-param name="string" select="substring-after($string,'&#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&#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
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
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