I used saxon v9 to profile an XSL transformation which converts XML to JSON. The profiler tells me that the function which escapes certain characters takes about 70% of the total processing time. The conversion is important because otherwise the created JSON file will be invalid because of characters that break the strings.
java -jar saxon9he.jar -xsl:jsontransform.xslt -s:input.xml -o:output.json -TP
The "method" used to escape the values looks like this:
<xsl:template name="escapejson">
<xsl:param name="string"/>
<xsl:sequence select="replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace($string, '\\','\\\\'),
'/', '\\/'),
'"', '\\"'),
'
','\\n'),
'
','\\r'),
'	','\\t'),
'\n','\\n'),
'\r','\\r'),
'\t','\\t')"/>
</xsl:template>
Unfortunately, my XSL knowledge is too limited to create an optimum solution on my own. I assume the repeated call of replace
is quite inefficient but don't know how to do it better.