HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

XSL template for converting XML to JSON

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
xsltemplatexmlforjsonconverting

Problem

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:


        
        
    


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.

Solution

I can believe it when the replace is the problem... it's creating a mess of nested code that's creating a lot of intermediate string values.

There's not much I can see to improve, though. The nesting can be reduced by combining a few of replaces...:

replace(.... , '\n|
', '\\n')


Doing the same for \t and \r will reduce six calls to just three.

Additionally, I would consider wrapping the entire replace function in a matches call, that matches any of the patterns you are searching for, so you only need to do the full replacement call stack on values that actually require it. A large matches for all replaced values will, depending on how often values are actually required to be replaced, save time.

Code Snippets

replace(.... , '\n|&#xA', '\\n')

Context

StackExchange Code Review Q#97580, answer score: 3

Revisions (0)

No revisions yet.