# xpath-next

> DOM 3 XPath implemention and helper for Node.js and the web

Latest version **0.0.40** (published 2023-05-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install xpath-next
pnpm add xpath-next
yarn add xpath-next
bun add xpath-next
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.40 |
| Published | 2023-05-30 |
| First published | 2023-03-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=18.15.0 |
| Dependencies | 0 |
| Unpacked size | 331 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Cameron McCormack |
| Maintainers | nikhilverma |
| Keywords | xpath, xml |

## Links

- npm: https://www.npmjs.com/package/xpath-next
- Repository: https://github.com/NikhilVerma/xpath-next
- Homepage: https://github.com/NikhilVerma/xpath-next#readme
- Issues: https://github.com/NikhilVerma/xpath-next/issues
- npm.io page: https://npm.io/package/xpath-next

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 0.0.40 (latest) — 2023-05-30
- 0.0.39 — 2023-05-29
- 0.0.38 — 2023-05-29
- 0.0.37 — 2023-05-29
- 0.0.36 — 2023-05-29
- 0.0.35 — 2023-05-29
- 0.0.34 — 2023-03-20
- 0.0.33 — 2023-03-08

## README

# xpath (next)

DOM 3 XPath 1.0 implemention and helper for JavaScript, with Node.js and Shadow DOM support.

Originally written by Cameron McCormack ([blog](http://mcc.id.au/xpathjs)).

Additional contributions from
Yaron Naveh ([blog](http://webservices20.blogspot.com/))
goto100
Thomas Weinert
Jimmy Rishe
and [others](https://github.com/NikhilVerma/xpath-next/graphs/contributors)

Further modifications by Nikhil Verma

- Support Shadow DOM
- Use ES6 module syntax

## Install

Install with [npm](http://github.com/isaacs/npm):

```sh
npm install xpath-next
```

xpath is xml engine agnostic but we recommend [xmldom](https://github.com/xmldom/xmldom):

```sh
npm install xmldom
```

## API Documentation

Can be found [here](https://github.com/NikhilVerma/xpath-next/blob/master/docs/xpath%20methods.md). See below for example usage.

## Your first xpath

```javascript
var xpath = require("xpath"),
	dom = require("xmldom").DOMParser;

var xml = "<book><title>Harry Potter</title></book>";
var doc = new dom().parseFromString(xml);
var nodes = xpath.select("//title", doc);

console.log(nodes[0].localName + ": " + nodes[0].firstChild.data);
console.log("Node: " + nodes[0].toString());
```

➡

    title: Harry Potter
    Node: <title>Harry Potter</title>

### Alternatively

Using the same interface you have on modern browsers ([MDN])

```javascript
var node = null;
var xml = "<book author='J. K. Rowling'><title>Harry Potter</title></book>";
var doc = new dom().parseFromString(xml);
var result = xpath.evaluate(
	"/book/title", // xpathExpression
	doc, // contextNode
	null, // namespaceResolver
	xpath.XPathResult.ANY_TYPE, // resultType
	null // result
);

node = result.iterateNext();
while (node) {
	console.log(node.localName + ": " + node.firstChild.data);
	console.log("Node: " + node.toString());

	node = result.iterateNext();
}
```

➡

    title: Harry Potter
    Node: <title>Harry Potter</title>

## Evaluate string values directly

```javascript
var xml = "<book><title>Harry Potter</title></book>";
var doc = new dom().parseFromString(xml);
var title = xpath.select("string(//title)", doc);

console.log(title);
```

➡

    Harry Potter

## Namespaces

```javascript
var xml = "<book><title xmlns='myns'>Harry Potter</title></book>";
var doc = new dom().parseFromString(xml);
var node = xpath.select("//*[local-name(.)='title' and namespace-uri(.)='myns']", doc)[0];

console.log(node.namespaceURI);
```

➡

    myns

## Namespaces with easy mappings

```javascript
var xml =
	"<book xmlns:bookml='http://example.com/book'><bookml:title>Harry Potter</bookml:title></book>";
var select = xpath.useNamespaces({ bookml: "http://example.com/book" });

console.log(select("//bookml:title/text()", doc)[0].nodeValue);
```

➡

    Harry Potter

## Default namespace with mapping

```javascript
var xml = "<book xmlns='http://example.com/book'><title>Harry Potter</title></book>";
var select = xpath.useNamespaces({ bookml: "http://example.com/book" });

console.log(select("//bookml:title/text()", doc)[0].nodeValue);
```

➡

    Harry Potter

## Attributes

```javascript
var xml = "<book author='J. K. Rowling'><title>Harry Potter</title></book>";
var doc = new dom().parseFromString(xml);
var author = xpath.select1("/book/@author", doc).value;

console.log(author);
```

➡

    J. K. Rowling

[MDN]: https://developer.mozilla.org/en/docs/Web/API/Document/evaluate

## License

MIT

---
_Source: https://npm.io/package/xpath-next · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
