Skip to main content

@dortdb/lang-xquery

An XQuery language plug-in for DortDB. It adds XQuery for tree-shaped and XML-like data, meaning DOM documents, parsed XML, and similar hierarchies. Navigate them with XPath paths and reshape them with FLWOR expressions.

Installation​

npm install @dortdb/core @dortdb/lang-xquery

@dortdb/core is a peer dependency.

Usage​

import { DortDB } from '@dortdb/core';
import { defaultRules } from '@dortdb/core/optimizer';
import { XQuery, DomDataAdapter } from '@dortdb/lang-xquery';

const db = new DortDB({
// in a browser, XQuery() picks up the global document on its own
mainLang: XQuery({ adapter: new DomDataAdapter(document) }),
optimizer: { rules: defaultRules },
});

const people = new DOMParser().parseFromString(
`<people>
<person age="30">Alice</person>
<person age="20">Bob</person>
</people>`,
'text/xml',
);
db.registerSource(['people'], people);

const result = db.query(
'for $p in $people/people/person[@age > 25] return string($p)',
);
// result.data -> ['Alice']

Data adapter​

The default adapter targets DOM and XML trees. It also reads JavaScript object properties through the attribute axis, as in obj/@prop. In the browser it uses the global document. In Node.js, supply a document from a DOM implementation such as jsdom and make the DOM globals available, or write a custom adapter for your own tree shape.

Dialect​

This is a subset of XQuery 3.0 for data selection: path expressions, FLWOR, and sequence operations. It leaves out the type system by design, so the as keyword, typeswitch, instance of, treat, castable, validate, function types other than function(*), and named function references with arity (fnname#arity) are unavailable, and there are no plans to add them. Of the type-related syntax, only cast expressions work.

These are missing today and may arrive later: try and catch, pragma expressions, the window FLWOR clause, inline functions, the %private and %public annotations, and module prolog features other than namespace definitions.

Custom plan operators​

XQuery extends the DortDB unified algebra with two operators.

OperatorDescription
ProjectionSizeAdds an attribute holding the total number of rows in the stream. It exposes $fs:last during path navigation.
TreeJoinImplements path steps such as a/b/c. It works like ProjectionConcat, except that its expression yields items and it supplies the XQuery focus per step ($fs:dot, $fs:position, $fs:last).

Both operators are proof that the algebra takes extensions. See Extending DortDB.

Documentation​

See the XQuery overview and the dialect reference, or the full docs at filipjezek.github.io/dortdb.

Modules​