Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert objects into nodes

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
like image 883
chriskelly Avatar asked Mar 13 '23 10:03

chriskelly


1 Answers

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)
)
like image 154
paxtonhare Avatar answered Mar 25 '23 09:03

paxtonhare