2
votes

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?

2

2 Answers

1
votes

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>`)
0
votes

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.