Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Remove HTML tags from XML output

ServiceNow SA
Kilo Guru

Hi team,

I have XML code to display some data in HTML knowledge article template.

However the output has <div> tags in it. How can i remove those?

Here is the sample code:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:evaluate object="true" var="jvar_statements">


var stmt = new GlideRecord('sn_compliance_policy_statement');
var qr = stmt.addJoinQuery('sn_compliance_m2m_policy_policy_statement', 'sys_id', 'content');
qr.addCondition('document', current.sys_id + '');
qr.orderBy('order');
stmt.query();

stmt;

<p>${current.policy_text}</p>

<j:while test="${jvar_statements.next()}">
<p style="font-weight: bold;"> ${jvar_statements.getValue('name')}</p>

<p>${jvar_statements.getValue('description')}</p>

</j:while>
</j:jelly>

Now the "description" is a HTML field and i need exactly the HTML output.

Expected:

Control: <<Some content>>
 
A. <<Some content>>
 
1. <<Some content>>
 
2. <<Some content>>
 
Actual:
 
<div>Control: <<Some content>> <div></div><div></div>A. <<Some content>><div></div>1. <<Some content>><div></div>2. <<Some content>><div></div>
 
Any help pls.
1 ACCEPTED SOLUTION

This is Jelly so you need to escape all of the chars that it does not like.  Here is a place to start with your reading.

https://docs.servicenow.com/bundle/newyork-application-development/page/script/server-scripting/refe...

 

View solution in original post

8 REPLIES 8

Omkar Mone
Mega Sage

Hi 

Can you try using the below regex in your code where you are getting the description - .replace(/<\/?[^>]+(>|$)/g, "");

 

Something like this - 

<p>${jvar_statements.getValue('description').toString().replace(/<\/?[^>]+(>|$)/g, "")}</p>

 

Hope this helps.

 

Regards

Omkar Mone

ServiceNow SA
Kilo Guru

When i am trying to save, getting below error:

"The content of elements must consist of well-formed character data or markup."

Hi 

That is because we did it in the <p> tag of html. Try this in the javascript script of yours, and set the processed value there in the <p> tag.

ServiceNow SA
Kilo Guru

This is my script. Where exactly should i put that? There is only XML part , nothing else. This is basically Knowledge Article Template (GRC)

find_real_file.png