Reputation: 5
I have this code:
<p>2 If the patient is unknown to the service a comprehensive assessment must be carried out prior to undertaking the procedure. (Reference <xref target="http://www.google.com" style="unformatted"/>)</p>
I want this xml to be like this:
<p><b>2</b>If the patient is unknown to the service a comprehensive assessment must be carried out prior to undertaking the procedure. (Reference <xref target="http://www.google.com" style="unformatted"/>)</p>
By using XSL 1.0 I have tried many ways to replace string but the node (xref) is removed!!
Upvotes: 0
Views: 56
Reputation: 167446
If you have written a template with match="p"
which does the string replacement then change that to have match="p/text()"
(or match="p//text()"
if you need the replacement in descendants too) and then make sure you have the identity transformation template in place:
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
It is not clear to me which substring exactly you want to replace or wrap with a b
element, an example of the approach suggested above is at http://xsltransform.net/bnnZVC, it does
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template name="wrap">
<xsl:param name="string" select="."/>
<xsl:param name="search"/>
<xsl:param name="wrap-name"/>
<xsl:choose>
<xsl:when test="not(contains($string, $search))">
<xsl:value-of select="$string"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before($string, $search)"/>
<xsl:element name="{$wrap-name}">
<xsl:value-of select="$search"/>
</xsl:element>
<xsl:call-template name="wrap">
<xsl:with-param name="string" select="substring-after($string, $search)"/>
<xsl:with-param name="search" select="$search"/>
<xsl:with-param name="wrap-name" select="$wrap-name"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p/text()">
<xsl:call-template name="wrap">
<xsl:with-param name="search" select="'2 '"/>
<xsl:with-param name="wrap-name" select="'b'"/>
</xsl:call-template>
</xsl:template>
</xsl:transform>
Upvotes: 2