-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
54 lines (37 loc) · 1.23 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
---
title: "Document: createExpression() method"
short-title: createExpression()
slug: Web/API/Document/createExpression
page-type: web-api-instance-method
browser-compat: api.Document.createExpression
---
{{APIRef("DOM")}}
This method compiles an {{DOMxRef("XPathExpression")}} which can then be used for (repeated) evaluations.
You must call this method on the same document that you run the expression against.
## Syntax
```js-nolint
createExpression(xpathText, namespaceURLMapper)
```
### Parameters
- `xpathText`
- : A string which is the XPath expression to be compiled.
- `namespaceURLMapper`
- : A function which maps a namespace prefix to a
namespace URL (or null if none needed).
### Return value
{{DOMxRef("XPathExpression")}}
## Examples
```js
const xpathExpr = document.createExpression("//div");
const xpathResult = xpathExpr.evaluate(document); // returns an XPathResult object
const nodeContext = document.querySelector("nav");
// Re-using the XPathExpression "xpathExpr"
const otherResult = xpathExpr.evaluate(nodeContext); // returns an XPathResult object
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- {{DOMxRef("Document.evaluate()")}}
- {{DOMxRef("XPathExpression")}}