fontoxpath
Advanced tools
Comparing version 3.12.0 to 3.12.1
@@ -149,17 +149,2 @@ | ||
/** | ||
* Can be used to signal an XPath program should executed | ||
*/ | ||
XPATH_3_1_LANGUAGE: Language.XPATH_3_1_LANGUAGE; | ||
/** | ||
* Can be used to signal an XQuery program should be executed instead | ||
* of an XPath | ||
*/ | ||
XQUERY_3_1_LANGUAGE: Language.XQUERY_3_1_LANGUAGE; | ||
/** | ||
* Can be used to signal Update facility can be used. | ||
* | ||
* To catch pending updates, use {@link evaluateUpdatingExpression} | ||
*/ | ||
XQUERY_UPDATE_3_1_LANGUAGE: Language.XQUERY_UPDATE_3_1_LANGUAGE; | ||
/** | ||
* Returns the result of the query, can be anything depending on the | ||
@@ -171,12 +156,5 @@ * query. Note that the return type is determined dynamically, not | ||
ANY_TYPE: ReturnType_2.ANY; | ||
ARRAY_TYPE: ReturnType_2.ARRAY; | ||
ASYNC_ITERATOR_TYPE: ReturnType_2.ASYNC_ITERATOR; | ||
/** | ||
* Resolve to a number, like count((1,2,3)) resolves to 3. | ||
*/ | ||
NUMBER_TYPE: ReturnType_2.NUMBER; | ||
/** | ||
* Resolve to a string, like //someElement[1] resolves to the text | ||
* content of the first someElement | ||
*/ | ||
STRING_TYPE: ReturnType_2.STRING; | ||
/** | ||
* Resolves to true or false, uses the effective boolean value to | ||
@@ -188,2 +166,10 @@ * determine the result. count(1) resolves to true, count(()) | ||
/** | ||
* Resolves to the first node.NODES_TYPE would have resolved to. | ||
*/ | ||
FIRST_NODE_TYPE: ReturnType_2.FIRST_NODE; | ||
/** | ||
* Resolve to an object, as a map | ||
*/ | ||
MAP_TYPE: ReturnType_2.MAP; | ||
/** | ||
* Resolve to all nodes the XPath resolves to. Returns nodes in the | ||
@@ -196,6 +182,16 @@ * order the XPath would. Meaning (//a, //b) resolves to all A nodes, | ||
/** | ||
* Resolves to the first node.NODES_TYPE would have resolved to. | ||
* Resolve to a number, like count((1,2,3)) resolves to 3. | ||
*/ | ||
FIRST_NODE_TYPE: ReturnType_2.FIRST_NODE; | ||
NUMBER_TYPE: ReturnType_2.NUMBER; | ||
/** | ||
* Resolve to an array of numbers | ||
*/ | ||
NUMBERS_TYPE: ReturnType_2.NUMBERS; | ||
/** | ||
* Resolve to a string, like //someElement[1] resolves to the text | ||
* content of the first someElement | ||
*/ | ||
STRING_TYPE: ReturnType_2.STRING; | ||
/** | ||
* Resolve to an array of strings | ||
@@ -205,12 +201,16 @@ */ | ||
/** | ||
* Resolve to an object, as a map | ||
* Can be used to signal an XPath program should executed | ||
*/ | ||
MAP_TYPE: ReturnType_2.MAP; | ||
ARRAY_TYPE: ReturnType_2.ARRAY; | ||
ASYNC_ITERATOR_TYPE: ReturnType_2.ASYNC_ITERATOR; | ||
XPATH_3_1_LANGUAGE: Language.XPATH_3_1_LANGUAGE; | ||
/** | ||
* Resolve to an array of numbers | ||
* Can be used to signal an XQuery program should be executed instead | ||
* of an XPath | ||
*/ | ||
NUMBERS_TYPE: ReturnType_2.NUMBERS; | ||
XQUERY_3_1_LANGUAGE: Language.XQUERY_3_1_LANGUAGE; | ||
/** | ||
* Can be used to signal Update facility can be used. | ||
* | ||
* To catch pending updates, use {@link evaluateUpdatingExpression} | ||
*/ | ||
XQUERY_UPDATE_3_1_LANGUAGE: Language.XQUERY_UPDATE_3_1_LANGUAGE; | ||
}; | ||
@@ -217,0 +217,0 @@ |
{ | ||
"name": "fontoxpath", | ||
"version": "3.12.0", | ||
"version": "3.12.1", | ||
"description": "A minimalistic XPath 3.1 engine in JavaScript", | ||
@@ -21,4 +21,2 @@ "main": "dist/fontoxpath.js", | ||
"prepare": "npm run build", | ||
"performance": "ts-node -P ./performance/tsconfig.json ./performance/runBenchmarks.ts", | ||
"performance-server": "rimraf ./performance/lib && tsc -p ./performance/web.tsconfig.json && concurrently -k -p \"[{name}]\" -n \"tsc,rollup,server\" \"tsc -p ./performance/web.tsconfig.json -w\" \"rollup -c ./performance/rollup.config.js -w \" \"ts-node -P ./performance/tsconfig.json ./performance/server.ts\"", | ||
"qt3performance": "ts-node -P test/tsconfig.json -r tsconfig-paths/register test/qt3testsBenchmark.ts", | ||
@@ -30,2 +28,3 @@ "qt3tests": "ts-mocha --paths -p test/tsconfig.json test/qt3tests.ts", | ||
"xqutstestsxqueryx": "ts-mocha --paths -p test/tsconfig.json test/xqutsTestsXQueryX.ts", | ||
"browsertests": "karma start", | ||
"lint": "tslint \"./src/**/*.ts\" --project . && prettier \"./src/**/*.ts\" --list-different" | ||
@@ -66,9 +65,6 @@ }, | ||
"devDependencies": { | ||
"@fontoxml/fonto-benchmark-runner": "0.0.4", | ||
"@microsoft/api-extractor": "^7.8.0", | ||
"@rollup/plugin-commonjs": "^11.1.0", | ||
"@rollup/plugin-node-resolve": "^7.1.3", | ||
"@tscc/tscc": "^0.4.8", | ||
"@types/benchmark": "^1.0.31", | ||
"@types/chai": "^4.2.11", | ||
"@types/glob": "^7.1.1", | ||
"@types/mocha": "^7.0.2", | ||
@@ -78,3 +74,2 @@ "@types/node": "^14.0.1", | ||
"@types/sinon": "^9.0.0", | ||
"benchmark": "^2.1.4", | ||
"chai": "^4.2.0", | ||
@@ -85,3 +80,8 @@ "concurrently": "^5.1.0", | ||
"fs-extra": "^9.0.0", | ||
"glob": "^7.1.6", | ||
"karma": "^5.0.9", | ||
"karma-chai": "^0.1.0", | ||
"karma-chrome-launcher": "^3.1.0", | ||
"karma-firefox-launcher": "^1.3.0", | ||
"karma-mocha": "^2.0.1", | ||
"karma-typescript": "^5.0.3", | ||
"mocha": "^7.1.1", | ||
@@ -92,3 +92,2 @@ "node-static": "^0.7.11", | ||
"prettier": "^2.0.5", | ||
"rollup": "^2.6.1", | ||
"sinon": "^9.0.2", | ||
@@ -95,0 +94,0 @@ "sinon-chai": "^3.5.0", |
160
README.md
# fontoxpath [](https://travis-ci.org/FontoXML/fontoxpath) [](https://david-dm.org/FontoXML/fontoxpath#info=devDependencies) [](http://badge.fury.io/js/fontoxpath) [](https://packagephobia.now.sh/result?p=fontoxpath) [](https://coveralls.io/github/FontoXML/fontoxpath?branch=master) [](https://snyk.io/test/github/FontoXML/fontoxpath?targetFile=package.json) | ||
A minimalistic [XPath 3.1](https://www.w3.org/TR/xpath-31/) and [XQuery 3.1](https://www.w3.org/TR/xquery-31/) engine for (XML) nodes with [XQuery Update Facility 3.0](https://www.w3.org/TR/xquery-update-30/#id-update-primitives) support. | ||
A minimalistic [XPath 3.1](https://www.w3.org/TR/xpath-31/) and [XQuery | ||
3.1](https://www.w3.org/TR/xquery-31/) engine for (XML) nodes with [XQuery Update Facility | ||
3.0](https://www.w3.org/TR/xquery-update-30/#id-update-primitives) support. | ||
@@ -31,12 +33,24 @@ [Demo page](https://xpath.playground.fontoxml.com) | ||
* `xpathExpression` `<String>` The query to evaluate. | ||
* `contextNode` `<Node>` The node in which context the `xpathExpression` will be evaluated. Defaults to `null`. | ||
* `domFacade` `<IDomFacade>` An [IDomFacade](src/domFacade/IDomFacade.ts) implementation which will be used for querying the DOM. Defaults to an implementation which uses properties and methods on the `contextNode` as described in the [DOM spec](https://dom.spec.whatwg.org/). | ||
* `variables` `<Object>` The properties of `variables` are available variables within the `xpathExpression`. Defaults to an empty `Object`. | ||
* `returnType` `<number>` Determines the type of the result. Defaults to `evaluateXPath.ANY_TYPE`. Possible values: | ||
* `evaluateXPath.ANY_TYPE` Returns the result of the query, can be anything depending on the query. Note that the return type is determined dynamically, not statically: XPaths returning empty sequences will return empty arrays and not null, like one might expect. | ||
* `contextNode` `<Node>` The node in which context the `xpathExpression` will be evaluated. Defaults | ||
to `null`. | ||
* `domFacade` `<IDomFacade>` An [IDomFacade](src/domFacade/IDomFacade.ts) implementation which will | ||
be used for querying the DOM. Defaults to an implementation which uses properties and methods on | ||
the `contextNode` as described in the [DOM spec](https://dom.spec.whatwg.org/). | ||
* `variables` `<Object>` The properties of `variables` are available variables within the | ||
`xpathExpression`. Defaults to an empty `Object`. | ||
* `returnType` `<number>` Determines the type of the result. Defaults to | ||
`evaluateXPath.ANY_TYPE`. Possible values: | ||
* `evaluateXPath.ANY_TYPE` Returns the result of the query, can be anything depending on the | ||
query. Note that the return type is determined dynamically, not statically: XPaths returning | ||
empty sequences will return empty arrays and not null, like one might expect. | ||
* `evaluateXPath.NUMBER_TYPE` Resolve to a `number`, like count((1,2,3)) resolves to 3. | ||
* `evaluateXPath.STRING_TYPE` Resolve to a `string`, like //someElement[1] resolves to the text content of the first someElement. | ||
* `evaluateXPath.BOOLEAN_TYPE` Resolves to a `boolean` true or false, uses the effective boolean value to determine the result. count(1) resolves to true, count(()) resolves to false. | ||
* `evaluateXPath.NODES_TYPE` Resolve to all nodes `Node[]` the XPath resolves to. Returns nodes in the order the XPath would. Meaning (//a, //b) resolves to all A nodes, followed by all B nodes. //*[self::a or self::b] resolves to A and B nodes in document order. | ||
* `evaluateXPath.FIRST_NODE_TYPE` Resolves to the first `Node` node.NODES_TYPE would have resolved to. | ||
* `evaluateXPath.STRING_TYPE` Resolve to a `string`, like //someElement[1] resolves to the text | ||
content of the first someElement. | ||
* `evaluateXPath.BOOLEAN_TYPE` Resolves to a `boolean` true or false, uses the effective boolean | ||
value to determine the result. count(1) resolves to true, count(()) resolves to false. | ||
* `evaluateXPath.NODES_TYPE` Resolve to all nodes `Node[]` the XPath resolves to. Returns nodes in | ||
the order the XPath would. Meaning (//a, //b) resolves to all A nodes, followed by all B | ||
nodes. //*[self::a or self::b] resolves to A and B nodes in document order. | ||
* `evaluateXPath.FIRST_NODE_TYPE` Resolves to the first `Node` node.NODES_TYPE would have resolved | ||
to. | ||
* `evaluateXPath.STRINGS_TYPE` Resolve to an array of strings `string[]`. | ||
@@ -48,11 +62,21 @@ * `evaluateXPath.MAP_TYPE` Resolve to an `Object`, as a map. | ||
* `options` `<Object>` Options used to modify the behavior. The following options are available: | ||
* `namespaceResolver` `<function(string):string?>` | ||
* `nodesFactory` `INodesFactory` A [INodesFactory](src/nodesFactory/INodesFactory.ts) implementation which will be used for creating nodes. | ||
* `language` `string` The query language to use. Defaults to `evaluateXPath.XPATH_3_1_LANGUAGE`. Possible values: | ||
* `evaluateXPath.XPATH_3_1_LANGUAGE` Evaluate `xpathExpression` according the [XPath spec](https://www.w3.org/TR/xpath-31/). | ||
* `evaluateXPath.XQUERY_3_1_LANGUAGE` Evaluate `xpathExpression` according the [XQuery spec](https://www.w3.org/TR/xquery-31/). | ||
* `namespaceResolver` `<function(string):string?>` By default, the namespaces in scope of the | ||
context item (if it is a node) are used. This is fine for most queries if you can assume how | ||
your XML uses prefixes. Use this function to override those namespaces to remove that | ||
assumption. This function will be called with a prefix (the empty string for the default | ||
namespaceURI) and should return a namespaceURI (or null for the null namespace). | ||
* `nodesFactory` `INodesFactory` A [INodesFactory](src/nodesFactory/INodesFactory.ts) | ||
implementation which will be used for creating nodes. | ||
* `language` `string` The query language to use. Defaults to | ||
`evaluateXPath.XPATH_3_1_LANGUAGE`. Possible values: | ||
* `evaluateXPath.XPATH_3_1_LANGUAGE` Evaluate `xpathExpression` according the [XPath | ||
spec](https://www.w3.org/TR/xpath-31/). | ||
* `evaluateXPath.XQUERY_3_1_LANGUAGE` Evaluate `xpathExpression` according the [XQuery | ||
spec](https://www.w3.org/TR/xquery-31/). | ||
* `moduleImports` `<Object<string, string>` | ||
* `debug` `<boolean>` If a debug trace should be tracked, see [debugging](#debugging) for more information. | ||
* `debug` `<boolean>` If a debug trace should be tracked, see [debugging](#debugging) for more | ||
information. | ||
* `logger` `<Object>` Object with functions used to override the standard logger. | ||
* `trace: <function(string):void>` The logger for the `trace()` function. The argument is the string of the original message. | ||
* `trace: <function(string):void>` The logger for the `trace()` function. The argument is the | ||
string of the original message. | ||
@@ -83,5 +107,4 @@ ### Example | ||
FontoXPath can output a basic trace for an error if the `debug` | ||
option is set to `true`. This is disabled by default because of | ||
performance reasons. | ||
FontoXPath can output a basic trace for an error if the `debug` option is set to `true`. This is | ||
disabled by default because of performance reasons. | ||
@@ -108,8 +131,9 @@ ```js | ||
Besides errors, the `fn:trace` function can be used to output information to the developer console. | ||
### Modifying XML | ||
Note: the use of XQuery Update Facility 3.0 is in preview and subject to change. | ||
To modify XML you can use [XQuery Update Facility 3.0](https://www.w3.org/TR/xquery-update-30/) as | ||
following | ||
To modify XML you can use [XQuery Update Facility 3.0](https://www.w3.org/TR/xquery-update-30/) as following | ||
```js | ||
@@ -119,3 +143,8 @@ evaluateUpdatingExpression(xpathExpression, contextNode, domFacade, variables, options); | ||
The arguments are the same as `evaluateXPath`. This returns a `Promise<Object>`, the object has a `xdmValue` and `pendingUpdateList`. The `xdmValue` is the result of query as if it was run using `evaluateXPath` with `evaluateXPath.ANY_TYPE` as `returnType`. The `pendingUpdateList` is an `<Object[]>` in which each entry represents an [update primitive](https://www.w3.org/TR/xquery-update-30/#id-update-primitives) where the `type` identifies the update primitive. | ||
The arguments are the same as `evaluateXPath`. This returns a `Promise<Object>`, the object has a | ||
`xdmValue` and `pendingUpdateList`. The `xdmValue` is the result of query as if it was run using | ||
`evaluateXPath` with `evaluateXPath.ANY_TYPE` as `returnType`. The `pendingUpdateList` is an | ||
`<Object[]>` in which each entry represents an [update | ||
primitive](https://www.w3.org/TR/xquery-update-30/#id-update-primitives) where the `type` identifies | ||
the update primitive. | ||
@@ -129,5 +158,10 @@ The pending update list can be executed using | ||
* `pendingUpdateList` `<Object[]>` The pending update list returned by `evaluateUpdatingExpression`. | ||
* `domFacade` `<IDomFacade>` See `evaluateXPath`. The default will use nodes from the `pendingUpdateList`. | ||
* `nodesFactory` `INodesFactory` A [INodesFactory](src/nodesFactory/INodesFactory.ts) implementation which will be used for creating nodes. Defaults to an implementation which uses properties and methods of nodes from the `pendingUpdateList`. | ||
* `documentWriter` `<IDocumentWriter>` An [IDocumentWriter](src/documentWriter/IDocumePntWriter.ts) implementation which will be used for modifying a DOM. Defaults to an implementation which uses properties and methods of nodes from the `pendingUpdateList`. | ||
* `domFacade` `<IDomFacade>` See `evaluateXPath`. The default will use nodes from the | ||
`pendingUpdateList`. | ||
* `nodesFactory` `INodesFactory` A [INodesFactory](src/nodesFactory/INodesFactory.ts) implementation | ||
which will be used for creating nodes. Defaults to an implementation which uses properties and | ||
methods of nodes from the `pendingUpdateList`. | ||
* `documentWriter` `<IDocumentWriter>` An [IDocumentWriter](src/documentWriter/IDocumePntWriter.ts) | ||
implementation which will be used for modifying a DOM. Defaults to an implementation which uses | ||
properties and methods of nodes from the `pendingUpdateList`. | ||
@@ -159,3 +193,3 @@ ### Example | ||
* `name` `<string>` The function name. | ||
* `name` `{namespaceURI: string, localName: string}` The function name. | ||
* `signature` `string[]` The arguments of the function. | ||
@@ -165,2 +199,64 @@ * `returnType` `string` The return type of the function. | ||
#### Example: | ||
```js | ||
const fontoxpath = require("fontoxpath") | ||
// Register a function called 'there' in the 'hello' namespace: | ||
fontoxpath.registerCustomXPathFunction({namespaceURI: 'hello', localName: 'there'}, ['xs:string'], 'xs:string', (_, str) => `Hello there, ${str}`); | ||
// and call it, using the BracedUriLiteral syntax (Q{}) | ||
const out = fontoxpath.evaluateXPathToString('Q{hello}there("General Kenobi")'); | ||
// Or by using a prefix instead: | ||
const URI_BY_PREFIX = {hi: 'hello'}; | ||
const out2 = fontoxpath.evaluateXPathToString( | ||
'hi:there("General Kenobi")', | ||
null, | ||
null, | ||
null, | ||
{namespaceResolver: (prefix) => (URI_BY_PREFIX[prefix])}); | ||
``` | ||
### Including modules | ||
Use the `registerXQueryModule` function to register an XQuery module. Registered modules will be | ||
globally available, but will have to be imported before they can be used. | ||
#### Example: | ||
```js | ||
const fontoxpath = require('fontoxpath'); | ||
fontoxpath.registerXQueryModule(` | ||
module namespace test = "https://www.example.org/test1"; | ||
declare %public function test:hello($a) { | ||
"Hello " || $a | ||
}; | ||
`); | ||
// Import the module using the XQuery way: | ||
fontoxpath.evaluateXPathToString(` | ||
import module namespace test = "https://www.example.org/test1"; | ||
(: Invoke the test:hello function :) | ||
test:hello('there') | ||
`, | ||
null, | ||
null, | ||
null, | ||
{language: fontoxpath.evaluateXPath.XQUERY_3_1_LANGUAGE} | ||
); | ||
// Or by using the moduleImports API, which can be used in XPath contexts as well | ||
fontoxpath.evaluateXPathToString(` | ||
(: Invoke the test:hello function :) | ||
test:hello('there') | ||
`, | ||
null, | ||
null, | ||
null, | ||
{moduleImports: { test: 'https://www.example.org/test1' } } | ||
); | ||
``` | ||
### Typescript | ||
@@ -192,4 +288,4 @@ | ||
The following features are unavailable at this moment, but will be implemented at some point in time ([and even | ||
sooner if you can help!](./CONTRIBUTING.md)): | ||
The following features are unavailable at this moment, but will be implemented at some point in time | ||
([and even sooner if you can help!](./CONTRIBUTING.md)): | ||
@@ -201,4 +297,6 @@ * Some DateTime related functions | ||
* The `treat as` operator | ||
* Some parts of FLWOR expressions | ||
For all available features, see the unit tests, or just try it out on the [Demo page](https://xpath.playground.fontoxml.com). | ||
For all available features, see the unit tests, or just try it out on the [Demo | ||
page](https://xpath.playground.fontoxml.com). | ||
@@ -205,0 +303,0 @@ ## Compatibility |
Sorry, the diff of this file is too big to display
426353
2311
305