3

I'm trying out the new server-side javascript API in MarkLogic 8. I'm mainly interested in search. The examples I've seen start by saving JSON into the database and then search the native JSON. That is not my use case because my company has tons of native XML already stored in the database. I want to use javascript to search for documents and I want the search results in JSON.

Is that even possible from javascript? When I run the code below, (modified from one of the examples) the result is an array of string, where each string is a complete XML document. Not what I want. Is there an API call to convert each result to JSON?

var count = 0;
var results = [];
for (var result of cts.search(cts.wordQuery("value1"))) {
    count++;
    results.push(result); 
};
results.push(fn.concat("Count = ", count));
results;

1 Answer 1

4

Yes, you can search and manipulate XML content via JavaScript, although in general JavaScript handles JSON better natively and XQuery handles XML better natively. What I mean is that the languages are designed around those data formats, not that there is anything you can't do in one or the other.

You don't need to translate XML to JSON (unless you really want to). The result of the search will be a document node, and you can use the DOM API, or any of the built-in functions that operate on nodes to process the results.

Sign up to request clarification or add additional context in comments.

2 Comments

For some examples, see the "Working With XML" section of the Server-side JavaScript feature introduction. For more detail, see XML DOM APIs in the JavaScript Reference Guide.
Thanks for your replies. @DaveCassel, your first link was very helpful. Using XPath on the search results is not difficult, and the resulting code is compact and IMO easier to read and understand than XQuery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.