Escaping characters in text output

Here should go questions about transforming XML with XSLT and FOP.
eivindan
Posts: 6
Joined: Mon Sep 29, 2008 2:32 pm

Escaping characters in text output

Post by eivindan »

Hi,

I'm trying to convert a convert a Office XML-file into a tagged text file. The text file uses a special syntax, similar to a flat XML-file (but it's not valid XML). That means that characters such as < and > have to be escaped in the output.

"<w:t>Some <tagged> text</w:t>" is now converted into "<tag:type>Some <tagged> text<tag:>" in the output. The wanted result is "<tag:type>Some \<tagged\> text<tag:>".

Any help on how I could accomplish this is much appreciated.

Eivind
katzlbt
Posts: 3
Joined: Fri Oct 17, 2008 11:49 am

Re: Escaping characters in text output

Post by katzlbt »

eivindan
Posts: 6
Joined: Mon Sep 29, 2008 2:32 pm

Re: Escaping characters in text output

Post by eivindan »

Just a followup with my solution, that also supports regexp search and replace.

Code: Select all

   <xsl:template name="filter_strings">
<xsl:param name="input_text" />
<xsl:param name="search" select="document('ITT_SEARCH_REPLACE.xml')/*/string_replacement/search" />
<xsl:variable name="replaced_text">
<xsl:value-of select="lpp:search-and-replace($input_text, $search[1]/find, $search[1]/replace)"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="$search[2]">
<xsl:call-template name="filter_strings">
<xsl:with-param name="input_text" select="$replaced_text" />
<xsl:with-param name="search" select="$search[position() > 1]" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$replaced_text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:function name="lpp:search-and-replace">
<xsl:param name="input"/>
<xsl:param name="search-string"/>
<xsl:param name="replace-string"/>
<xsl:sequence select="replace($input, $search-string, $replace-string)"/>
</xsl:function>
ITT_SEARCH_REPLACE.xml:

Code: Select all

<root>
<string_replacement>
<search>
<find>></find>
<replace>\\></replace>
</search>
<search>
<find><</find>
<replace>\\<</replace>
</search>
</string_replacement>
</root>
Post Reply