cancel
Showing results for 
Search instead for 
Did you mean: 

How to HTML encode

DarylBeaumont
New Contributor

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.

2 REPLIES 2

Steve
Former Employee

Have you tried using the encodeURIComponent() function? E.g. encodeURIComponent(“sam & max”)

tstack
Former Employee

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('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;')

Or, the slightly fancier:

$msg.replace(/&|<|>/g, m => match m { '&' => '&amp;', '<' => '&lt;', '>' => '&gt;' })