I’m trying to develop an XSLT stylesheet which transforms a given DocBook document to a file which can be fed to the lout document formatting system (which then generates PostScript output).
Doing so requires that I replace a few characters in the text of DocBook elements because they have a special meaning to lout. In particular, the characters
/ | & { } # @ ~ \ "
need to be enclosed in double quotes (“) so that lout treats them as ordinary characters.
For instance, a DocBook element like
<para>This is a sample {a contrived one at that} ~ it serves no special purpose.</para>
should be transformed to
@PP
This is a sample "{"a contrived one at that"}" "~" it serves no special purpose.
How can I do this with XSLT? I’m using xsltproc, so using XPath 2.0 functions is not an option but a number of EXSLT functions are available.
I tried using a recursive template which yields the substring up to a special character (e.g. {), then the escaped character sequence ("{") and then calls itself on the substring after the special character. However, I have a hard time making this work properly when trying to replace multiple characters, and one of them is used in the escaped sequence itself.
I. This is most easily accomplished using the
str-maptemplate of FXSL:when this transformation is aplied on the provided XML document:
the wanted, correct result is produced:
@PP
This is a sample “{“a contrived one at that”}” “~” it serves no special purpose.
II. With XSLT 1.0 recursive named template: