I'd like to work with some document properties (which are in xml) as nodes so I can work with child elements. So far I have:
var counts = [];
for (var p of xdmp.documentProperties(uris)) {
var isANode = p instanceof Node; // false (actually true - see edit)
var count = xdmp.xqueryEval('fn:count(' + p + '//data)')
counts.push(count)
}
counts
I've seen nodebuilder examples in the MarkLogic documentation and I'd like to use that interface. e.g.
myNode.xpath('//data')
var children = myNode.childNodes()
Instead of evaluating using XPath is there a way to I convert the object into a node? Alternatively, is there a better way to work with existing XML in Server Side JavaScript?
In XQuery I can use xdmp:tranform-to-json()
but that function is not provided in SJS.
EDIT:
In my attempt to provide the a clean code sample I left out vital information. I had my code structured as follows:
for (var uri of cts.uriMatch('/pattern/*')) {
var p = xdmp.documentProperties(uri);
var isANode = p instanceof Node; // false
// ...
}
instead of
for (var p of xdmp.documentProperties(cts.uriMatch('/pattern/*'))) {
var isANode = p instanceof Node; // true
// ...
}
The type returned is a ValueIterator
which equivalent of an XQuery sequence and can be accessed using:
xdmp.documentProperties(uri).next().value
This worked for me in QConsole:
var uris = ['/test.xml', '/test2.xml'];
var counts = [];
for (var p of xdmp.documentProperties(uris)) {
counts.push(fn.count(p.xpath('//data')))
}
counts
In my test each p was a Node. I created some sample data with an xquery script.
let $test-data := (
<priority>
<data>hello</data>
</priority>,
<status>
<data>hi</data>
</status>
)
return
(
xdmp:document-set-properties("/test.xml", $test-data),
xdmp:document-set-properties("/test2.xml", $test-data)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With