I need to replace characters ' and " using xslt. I need to replace them in whole text in xml file. '
character should be replaced to "
but only for expressions, in cases where '
is part of word ('s, 'm, 've, etc.) it should not be replaced.
<para ampexmnem="dpa2">
<paratext>The Secretary, in consultation with the Secretary of Health and Human Services, shall, with respect to any items described in this subsection which are to be included in a taxpayer's return of tax, develop language for such items which is as simple and clear as possible (such as referring to 'insurance affordability programs' as 'free or low-cost health insurance').</paratext>
</para>
I tried different solutions, but have no luck, characters were not replaced.
<xsl:template name="replace-quotes">
<xsl:param name="text"/>
<xsl:param name="replace" select="'&apos;'"/>
<xsl:param name="by" select="'&quot;'"/>
<xsl:choose>
<xsl:when test="contains($text, $replace)">
<xsl:value-of select="substring-before($text, $replace)"/>
<xsl:value-of select="$by"/>
<xsl:call-template name="replace-quotes">
<xsl:with-param name="text" select="substring-after($text, $replace)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="text()">
<xsl:call-template name="replace-quotes">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:choose>
<xsl:when test="contains(string-join(text(), ''), '&apos;')">
<xsl:value-of select="replace(string-join(text(), ''), '&apos;', '&quot;')"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
May be I missed template's order, but I tried to put these templates in the beginning and in the end of xslt.
Actual result is: The Secretary, in consultation with the Secretary of Health and Human Services, shall, with respect to any items described in this subsection which are to be included in a taxpayer's return of tax, develop language for such items which is as simple and clear as possible (such as referring to 'insurance affordability programs' as 'free or low-cost health insurance').
Expected result is: The Secretary, in consultation with the Secretary of Health and Human Services, shall, with respect to any items described in this subsection which are to be included in a taxpayer's return of tax, develop language for such items which is as simple and clear as possible (such as referring to "insurance affordability programs" as "free or low-cost health insurance").
I don't think the problem is defined well enough.
Ostensibly you want to replace an apostrophe with a quote only if it appears at the beginning or at the end of a word - which could be done using:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node() except text()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="replace(., '(^|\W)''(\w)|(\w)''(\W|$)', '$1$3"$2$4')"/>
</xsl:template>
</xsl:stylesheet>
But then consider what will happen if you have text like:
an angle of 135° 30' 0"
or:
it is the parents' responsibility