EDIT 1
I just realized Dimitre's version uses recursion and is quite similar; so my opening sentence seems silly now.
Here's a version that uses recursion:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="fld-beg" select="'[['"/>
<xsl:variable name="fld-end" select="']]'"/>
<xsl:variable name="replacement" select="'F'"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="source/text()">
<xsl:call-template name="replace">
<xsl:with-param name="str" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template name="replace">
<xsl:param name="str"/>
<xsl:choose>
<xsl:when test="contains($str, $fld-beg) and contains($str, $fld-end)">
<xsl:call-template name="replace">
<xsl:with-param name="str" select="concat(
substring-before($str, $fld-beg),
$replacement,
substring-after($str, $fld-end))"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$str"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
match="source/text()"
matches all the text in 'source' node as one string and passes it to the named pattern 'replace'. 'replace' looks for occurrences of the beginning and ending delimiters ('[[' and ']]'), and if found splits the text at (and thus ignoring) the delimiters, inserts the replacement string, and passes all that to itself to repeat the process.
I say "split", but given the lack of a real split()
in XPath 1.0, we can get by teaming up substring-before()
and substring-after()
.
Given the text in the source, 'abc [[field1]] def [[field2]] ghi'
, the recursion goes like this, showing how it's split, replaced, and passed:
'abc ' + 'F' + def [[field2]] ghi'
, passed again into 'replacement'
'abc F def ' + 'F' + ' ghi'
, passed again into 'replacement'
- since the delimiters are not present,
'abc F def F ghi'
is passed back up to match="source/text()"
Here's how it looks with xsltproc
:
$ xsltproc so.xsl so.xml
<?xml version="1.0"?>
<xliff xmlns:xliff="urn:oasis:names:tc:xliff:document:1.1" version="1.1">
<file>
<source>abc F def F ghi</source>
</file>
</xliff>
I hope this helps.