Last active
December 14, 2018 18:29
-
-
Save jmakeig/9a6aad1ebbe393128d5a643914619a32 to your computer and use it in GitHub Desktop.
Tagged template literal to build XML Nodes in MarkLogic.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
function xml(strings, ...values) { | |
let str = ''; | |
for (let i = 0; i < strings.length; i++) { | |
str += strings[i] + (values[i] || ''); | |
} | |
return fn.head( | |
xdmp.unquote(str, null, 'format-xml') // Sequence | |
); // Document | |
} | |
// Usage: | |
const name = 'Foo Bar'; | |
const color = 'orange'; | |
const doc = xml` | |
<product xmlns="products"> | |
<name>${name}</name> | |
<color>${color}</color> | |
</product> | |
`; | |
console.assert(doc instanceof Document); | |
doc; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment