Search code examples
javascriptxquerymarklogic

XQuery sequence equivalent in MarkLogic Server-Side JavaScript


I adapted the code below from some XQuery. However, when I run it, it only inserts the document into collection B, not collections A and B as the equivalent XQuery does.

declareUpdate();
xdmp.documentInsert(
  'mydoc.xml',
  fn.head(xdmp.unquote(`<mydoc/>`)),
  xdmp.defaultPermissions(),
  ('A', 'B')
);

Solution

  • The problem is the ('A', 'B'). In XQuery that represents a sequence of two strings. In JavaScript, that’s interpreted as two statements and returns the result of the last one, in this case 'B'. Thus the syntax is valid—which is why there’s no error—but the intent is different. In general, the equivalent of an XQuery sequence in JavaScript is an array. Thus the code above should be

    declareUpdate();
    xdmp.documentInsert(
      'mydoc.xml',
      fn.head(xdmp.unquote(`<mydoc/>`)),
      xdmp.defaultPermissions(),
      ['A', 'B']
    );
    

    Note the square brackets on ['A', 'B']. This is something to watch out for when copy-pasting from XQuery to JavaScript.