2

I am using javascript to read an XML, and create an envelope before storing in database. While creating headers, I need to populate the element "created-by" with the value of current user.

let user = xdmp.getCurrentUser()

I tried something like below, but its not replacing the variable with the value.

let a = xdmp.unquote('<created-by>{user}</created-by>')

How could I pass variable to XML using Javascript?

Bhanu
  • 427
  • 2
  • 8

2 Answers2

1

If you want to use JavaScript template literals, then change the single quote ' to a back-tick `, and put a $ in front of the curly braces marking the variable placeholder:

let a = xdmp.unquote(`<created-by>${user}</created-by>`)
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • Perfect! Thanks a lot. I also noticed that I do not have to convert headers to XML. I was able to call dhf.makeEnvelope with XML document, headers (JSON) and dataFormat as 'xml'. – Bhanu Feb 20 '19 at 21:52
0

There’s also the NodeBuilder API. It allows you to programmatically build XML from JavaScript. That’s probably overkill for what you have above, but good when you need more control over the structure of the XML you’re constructing.

Justin Makeig
  • 2,097
  • 15
  • 29