1

I need to filter a MarkLogic sequence (after the cts query) with javascript by checking the existence of an element. If the element exists, show the value. I know this is possible with XQuery by doing something like

fn:filter(function($a) { fn:not(fn:empty($a/es:envelope/es:instance/MyEntity/MyField)) }, $miseq)/es:envelope/es:instance/MyEntity/MyField

I didn't see an equivalent for Javascript. I know i can achieve this doing it manually with a for loop, but performance is significantly worse (not to mention is less pretty)

Any suggestion/s?

Thank you

MGS86
  • 13
  • 2

2 Answers2

1

A Sequence is an instance of Iterable as explained in the JavaScript reference guide. So, you could just iterate it like other iterables in JavaScript. Something like:

var result = [];

for (const a of miseq) {
   var myField = a.xpath('/es:envelope/es:instance/MyEntity/MyField');

   if (fn.exists(myField)) {
     result = result.concat(myField.toArray()); // you may be pulling more than one element
   }
}

HTH!

grtjn
  • 20,254
  • 1
  • 24
  • 35
  • const a *of* miseq? – BenW May 15 '19 at 00:37
  • Thanks for your comment grtjn. Yes, i knew this was possible with a for loop but based on a couple tests i did, performance is significantly worst than doing with the XQuery approach so i was wondering it there was an equivalent for javascript. It seems there isn't. Thank you! – MGS86 May 16 '19 at 04:24
  • @MGS86, whether XQuery performs better or not depends on the full picture. Where is your sequence coming from, and what are you doing with it? If you have many results, and you are going through all, that will take a lot of time anyhow. I could imagine that with the natural integration of XPath in XQuery, the optimizer can optimize XQuery code much easier than JavaScript, but it most cases you can write JavaScript code that is equal in performance. – grtjn May 16 '19 at 06:46
  • "so i was wondering it there was an equivalent for javascript. It seems there isn't. " @grtjn's answer is exactly that "an equivalent for javascript". Any closer and it would be "equals" in which case javascript would have to = xquery. – DALDEI May 27 '19 at 12:06
0

You know how to do this from XQuery. You can invoke xquery from javascript. If theres strong enough rationale to do so (such as performance), I suggest you try what you know. Javascript does not have the core language concept of sequences that XQuery does, so what can be done in XQuery as 'core' language statements cannot always be done in JavaScript as well (and visa versa). Support for JavaScript doesn't imply that there are not cases where XQuery may be the better option (or visa versa). Support for invoking XQuery from JavaScript does imply that this use case is considered reasonable (and visa versa). Polyglot is not a expletive.

DALDEI
  • 3,722
  • 13
  • 9
  • Thanks DALDEI. I haven't done this before but since there isn't seem to be an equivalent for Javascript i will give it a try. – MGS86 May 16 '19 at 04:30