I am defining a JavaScript variable from XSLT and am getting an error due to an unescaped string. I understand that I need to replace this to '
, but I'm unsure how to do that in XSLT 1.0.
XSLT example:
var currentComment = '<xsl:value-of select="root/Reviews/Review/Comment" />';
Rendered javascript with unescaped single quote:
var currentComment = 'Let's test a string.',
// Causing an error ------^
This is just an implementation of the JLRische-Novachev solution I did to accomplish my xml. Maybe it can be usefull:
xsl:
Output:
As Ian Roberts pointed out in his comment, you need to account for backslashes and not just apostrophes. Dimitre's answer can be modified as follows to account for this:
Any time you need to escape something, you can just use
apply-templates
on it with modeescape
. For this input XML:This produces:
This transformation:
when applied on any XML document (not used), produces the wanted, correct result: