Server-side JavaScript

Working with XML

Everything’s a node in a MarkLogic database: JSON, XML, binary, and text. JavaScript provides syntax for working with JSON nodes, mapping them to plain old JavaScript objects. No such equivalent is built into the language for XML. The NodeBuilder API provides a lightweight interface for constructing XML nodes from JavaScript. XML nodes can also be constructed from strings using xdmp.unquote(). Like a JSON node, an XML node can be saved in the database or manipulated using either Server-side JavaScript or XQuery. XML nodes support a read-only W3C DOM interface for reading and traversing node structures. Additionally, any XDM function that takes a node as its first parameter in XQuery can also be called as property of an XMLNode.

XPath

Because JSON nodes in the database are nodes, in the XML Document Model (XDM) sense, you can access them with XPath. This provides a powerful way to drill down into XML content. To use this from Server-side JavaScript, you can call xdmp.xqueryEval(), passing in an expression, or apply the xpath() function to an XMLNode. The code above has an example of using the .xpath() function.

From XPath, JSON properties look like un-namespaced XML elements. MarkLogic also extends the XDM with object-node() and array-node() types.

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.