09-24-2018 05:30 AM
xml entity_2018_09_24.slp (9.3 KB)
Hi,
We recently encountered an issue involving passing a variable to an XML generator. The variable contains an ampersand (‘&’), which causes the XML generator to throw the error ‘The entity name must immediately follow the “&” in the entity reference.’ This is, of course, because the ampersand is not being encoded as ‘&’. We’ve been unable to find a way to automatically convert special characters such as ‘&’, ‘>’, ‘<’ to their HTML counterpart. Can anyone suggest a way to do so other than using the Script Snap?
I’ve attached an example pipeline where the XML generator fails due to lack of encoding and succeeds when encoded.
09-24-2018 11:01 PM
Have you tried using the encodeURIComponent() function? E.g. encodeURIComponent(“sam & max”)
09-25-2018 07:50 AM
The next release of the platform will have an expression language function for converting characters to their HTML entities. Until then, I think most people use replace() to do the substitution on the string, like so:
$msg.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>')
Or, the slightly fancier:
$msg.replace(/&|<|>/g, m => match m { '&' => '&', '<' => '<', '>' => '>' })