DOM 3 and 4 XPath 1.0 implemention for browser and Node.js environment.
npm install xpath-tsDOM 3 and 4 XPath 1.0 implemention for browser and Node.js environment with support for custom Function, Variable and Namespace mapping.
See CHANGELOG.md
Install with npm:
```
npm install xpath-ts
This library is xml engine agnostic but I recommend to use xmldom-ts, xmldom or jsdom
``
npm install xmldom-ts
or
``
npm install xmldom
or
``
npm install jsdom
Can be found here. See below for example usage.
`typescript
import { DOMParserImpl as dom } from 'xmldom-ts';
import * as xpath from 'xpath-ts';
const xml = '
const doc = new dom().parseFromString(xml);
const nodes = xpath.select('//title', doc);
console.log(nodes[0].localName + ': ' + nodes[0].firstChild.data);
console.log('Node: ' + nodes[0].toString());
`
➡
``
title: Harry Potter
Node:
Using the same interface you have on modern browsers ([MDN])
`typescript
import { DOMParserImpl as dom } from 'xmldom-ts';
import * as xpath from 'xpath-ts';
const xml = "
const doc = new dom().parseFromString(xml);
xpath.installDOM3XPathSupport(doc);
const result = doc.evaluate(
'/book/title', // xpathExpression
doc, // contextNode
null, // namespaceResolver
xpath.XPathResult.ANY_TYPE, // resultType
null // result
);
let node = result.iterateNext();
while (node) {
console.log(node.localName + ': ' + node.firstChild.data);
console.log('Node: ' + node.toString());
node = result.iterateNext();
}
`
➡
``
title: Harry Potter
Node:
`typescript
import { DOMParserImpl as dom } from 'xmldom-ts';
import * as xpath from 'xpath-ts';
const xml = '
const doc = new dom().parseFromString(xml);
const title = xpath.select('string(//title)', doc);
console.log(title);
`
➡
``
Harry Potter
`typescript
import { DOMParserImpl as dom } from 'xmldom-ts';
import * as xpath from 'xpath-ts';
const xml = "
const doc = new dom().parseFromString(xml);
const node = xpath.select("//*[local-name(.)='title' and namespace-uri(.)='myns']", doc)[0];
console.log(node.namespaceURI);
`
➡
``
myns
`typescript
import { DOMParserImpl as dom } from 'xmldom-ts';
import * as xpath from 'xpath-ts';
const xml = "
const select = xpath.useNamespaces({ bookml: 'http://example.com/book' });
console.log(select('//bookml:title/text()', doc)[0].nodeValue);
`
➡
``
Harry Potter
`typescript
import { DOMParserImpl as dom } from 'xmldom-ts';
import * as xpath from 'xpath-ts';
const xml = "
const select = xpath.useNamespaces({ bookml: 'http://example.com/book' });
console.log(select('//bookml:title/text()', doc)[0].nodeValue);
`
➡
``
Harry Potter
`typescript
import { DOMParserImpl as dom } from 'xmldom-ts';
import * as xpath from 'xpath-ts';
const xml = "
const doc = new dom().parseFromString(xml);
const author = xpath.select1('/book/@author', doc).value;
console.log(author);
`
➡
``
J. K. Rowling
[mdn]: https://developer.mozilla.org/en/docs/Web/API/Document/evaluate
#### Download
``
git clone 'https://github.com/backslash47/xpath-ts'
cd xpath-ts
#### Install
``
npm install
#### Build
``
npm run build
You will get the transpiled code under '/dist/lib' and typings under '/dist/types'.
#### Test
Run standard tests with Mocha + Chai testing framework
```
npm test
- Cameron McCormack - _Initial work_ - blog
- Yaron Naveh - blog
- goto100
- Jimmy Rishe
- Thomas Weinert
- Matus Zamborsky - _TypeScript rewrite_ - Backslash47
- Others - others
This project is licensed under the MIT License - see the LICENCE.md file for details.