EDIT 1
I just realized that the version of Dimitre uses recursion and is very similar; therefore my opening sentence seems silly now.
Here is the version using 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 the 'source' node as one line and passes it to the named replace template. 'replace' looks for occurrences at the beginning and end of the delimiters ('[[' and ']]'), and if the one found breaks the text into (and thus ignores) the delimiters, inserts a replacement string and passes it all to itself to repeat the process.
I say split, but given the lack of real split() in XPath 1.0, we can combine substring-before() and substring-after() .
Given the text in the source, 'abc [[field1]] def [[field2]] ghi' , the recursion looks like this: it is shown how it is broken, replaced and passed:
'abc ' + 'F' + def [[field2]] ghi' , again moved to' replacement ''abc F def ' + 'F' + ' ghi' , again switched to 'replacement'- since there are no delimiters,
'abc F def F ghi' is passed back to match="source/text()"
Here's what it looks like 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>
Hope this helps.
source share