Escape XML Characters

mdidrikson
Kilo Contributor

Hello,

I have an XML SOAP request that gets sent out regularly to an external vendor. Our vendor is saying that we need to HTML encode our apostrophes in our messages. These show up in user last names such as O'Donnell.

I have tried to replace the apostrophe characters using the following code:



//FILTER OUT SPECIAL CHARACTERS FOR XML REQUEST
var apostrophe = "'";
defaultReporterFirstName = defaultReporterFirstName.replace(/'/g, apostrophe);
defaultReporterLastName = defaultReporterLastName.replace(/'/g, apostrophe);



The issue now is it appears that Service Now is HTML encoding the ampersand character at the beginning of the string, but this is breaking the HTML encoding for the apostrophe.

O'DonnellWeb Ticket

Is there a way I can HTML encode apostrophe characters in XML?

Thanks!

Mark Didrikson

6 REPLIES 6

mdidrikson
Kilo Contributor

Here is the XML output:



<ReporterLastName>O&apos;Donnell</ReporterLastName><Origin>Web Ticket</Origin>


Thanks!

Mark Didrikson


One trick I do when having to hard code an escape in javascript code that is stored in service-now is to write it like this:

[code lang='javascript']
var apostrophe = "&"+"apos;";
[/code]

This will prevent the script saving action from converting the string to an actual apostrophe.


Thanks John. I tried that out, but I'm still getting the same issue.



<ReporterLastName>O&apos;Donnell</ReporterLastName><Origin>Web Ticket</Origin>




//FILTER OUT SPECIAL CHARACTERS FOR XML REQUEST
var apostrophe = "&"+"apos;";
defaultReporterFirstName = defaultReporterFirstName.replace(/'/g, apostrophe);
defaultReporterLastName = defaultReporterLastName.replace(/'/g, apostrophe);


This is occurring in a Business Rule, if that matters.

Thanks!


Try:

[code lang='javascript']
var apostrophe = "&apos;";
[/code]

OR

[code lang='javascript']
var apostrophe = "'";
[/code]

See if either of those work.