Reputation: 9
I want to limit the output of an XSLT element to 2000 chars. The element has many IF conditions and depending upon which conditions are true the sub-elements are displayed. I cannot use concat() function as the variable is re-initialized in every IF block. I cannot use substring() as there are many sub-elements and i want the concatenation of all sub-elements to be 2000 chars. I am currently working on javascript option but without much luck. I am a newbie to XSLT so any help much appreciated.
Thanks, Clayton
Upvotes: 0
Views: 2553
Reputation: 163458
Both Greg and Kevan have given you examples of the right design here: do the first transformation without truncation, then do the truncation in a second transformation.
The only question mark over this is performance. If the first transformation is going to generate 50Mb of output and you only want the first 50K, this isn't an efficient approach. If at all possible, rewrite the requirements to express them in terms of the size of the input rather than the size of the output. If you can't do that, and if the two-phase approach is too wasteful, then the only solution I can think of is to inject some stateful extension functions that monitor how much output is being produced and allow you to ask whether the limit has been exceeded. Details will depend on your choice of processor.
Upvotes: 0
Reputation: 891
Wouldn't this approach work. Do the transform as you require it to work but leave the result in a variable rather than in the output doc. Then put a substring of this into the output
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:variable name="normalResult">
<!-- Put your basic transform code here -->
</xsl:variable>
<xsl:template match="/">
<TrimmedResult>
<xsl:value-of select="substring($normalResult, 1, 2000)"/>
</TrimmedResult>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Reputation: 993901
It's unlikely that you are going to be able to do this in pure XSLT in a nice way. Instead, I would recommend writing a different program that runs after your XSLT transformation, that truncates any long elements in the resulting XML to 2000 characters.
Upvotes: 0