Forum Discussion
@mbowen Hi Matthew. I hope you are well. Earlier this summer you offered me a solution to get past my XML HTML entity declaration issues. We basically Base64 encoded the value of the XML element that contained my HTML entity codes, passed it through the both the XML Generator and XML Formatter snaps, and then just before we wrote the XML document to file, we used a Mapper snap with the following expression in it.
$content.replace(/<(short|long)-description>(.+?)</\1-description>/g,
(_matched, prefix, b64enc) => “<”+prefix+“-description>”+Base64.decode(b64enc)+“</”+prefix+“-description>”)
I now have another element in the same XML document that I wish to do a Base64.decode() on, however, I’m having trouble understanding how your replace method and the callback function within it works. Could you help me to understand it so that I can string an additional replace on this same expression?
I’m basically trying to decode the value of the custom-attribute below, but when I try to emulate the pattern that you established, I can’t get it to work.
<custom-attribute attribute-id="productFeatures">PCFbQ0RBVEFbPHA+RmVhdHVyZXMgTGlzdDwvcD5dXT4=</custom-attribute>
Hey @alex.panganiban.guild . Hope you’ve been well too. I will look at this today.
- alex_panganiban5 years agoContributor
@mbowen Good Morning, Matthew. Well, I came up with a solution, yet I still don’t understand how this expression works. I just blindly and dumbly followed the pattern more closely. Mostly the whole (_matched, prefix, b64enc) callback function escapes my comprehension. This is what I finally came up with and it seems to work. If I have any new custom attributes in the future that contain HTML entity codes, I can just add them to prefix options in this expression.
$content.replace(/<(short|long)-description>(.+?)</\1-description>/g,
(_matched, prefix, b64enc) => “<”+prefix+“-description>”+Base64.decode(b64enc)+“</”+prefix+“-description>”).replace(/(productFeatures|anyNewCustomAttributeWithEncodedHTMLAddHere)">(.+?)</custom-attribute>/g, (_matched, prefix, b64enc) => prefix + “">” + Base64.decode(b64enc) + “”)Wonderful, but yikes. Ok, let me parse your expression and we’ll explain together.
- alex_panganiban5 years agoContributor
@mbowen Haha, I know it looks scary, but hey, when you originally came up with this solution, I thought it was brilliant and imaginative…and I still do. It works wonderfully and it solved the HTML entity problem I was having with XML. And we were able to do it without having to complicate doctypes and DTD’s. So kudos to you. Now I just wish I comprehended how it works.
I will add, I had to place the element’s value in a CDATA wrap in order to get the XML document to import into Salesforce successfully. Before I did that, the Salesforce XML validation process was also flagging my undeclared HTML entity codes and rejecting my import.
What I ended up finally importing, after Base64 decoding my value with the replacement expression, looked something like what’s below. I did the same wrap thing with the short and long descriptions that we worked on earlier this summer as well.
<custom-attribute attribute-id=“productFeatures”><![CDATA[<p>Features List ™</p>]]></custom-attribute>