Tutan Ramen
Tutan Ramen

Reputation: 1242

MarkLogic node.js Client API - Query by element value with XML namespace

Say I have a collection with records structured like so:

<m:m xmlns:m="http://www.m.com/">
    <m:data>
        <z:term xmlns:z="http://z.come/schema/index.html#">
            <z:name>abcd</z:name>
            <z:id>123456789</z:id>
......

And then I want to select records where z:id = whatever or z:name = whatever. How can I do this with queryBuilder?

I've been able to successfully make such queries when there is no namespace. With the namespaces it doesn't seem to work the same way.

Upvotes: 1

Views: 135

Answers (1)

Dave Cassel
Dave Cassel

Reputation: 8422

You need to specify the target element using QueryBuilder.element(). The documentation for element() says there are three ways to specify the element's QName:

A name without a namespace can be expressed as a string. A namespaced name can be expressed as a two-item array with uri and name strings or as an object returned by the queryBuilder#qname function.

var ml = require('marklogic');
var conn = require('./config.js').connection;
var db = ml.createDatabaseClient(conn);
var qb = ml.queryBuilder;

db.documents.query(
  qb.where(
    qb.value(qb.element(['http://z.come/schema/index.html#', 'id']), '123456789')
  )
).result()
.then(function(docs) {
  console.log('This search found: ' + JSON.stringify(docs[0]));
})
.catch(function(error) {
  console.log('something went wrong: ' + error);
});

Upvotes: 3

Related Questions