| 1 |
# XPath |
| 2 |
|
| 3 |
[](https://godoc.org/github.com/antchfx/xpath) |
| 4 |
[](https://coveralls.io/github/antchfx/xpath?branch=master) |
| 5 |
[](https://github.com/antchfx/xpath/actions/workflows/testing.yml) |
| 6 |
[](https://goreportcard.com/report/github.com/antchfx/xpath) |
| 7 |
|
| 8 |
XPath is Go package provides selecting nodes from XML, HTML or other documents using XPath expression. |
| 9 |
|
| 10 |
# Implementation |
| 11 |
|
| 12 |
- [htmlquery](https://github.com/antchfx/htmlquery) - an XPath query package for HTML document |
| 13 |
|
| 14 |
- [xmlquery](https://github.com/antchfx/xmlquery) - an XPath query package for XML document. |
| 15 |
|
| 16 |
- [jsonquery](https://github.com/antchfx/jsonquery) - an XPath query package for JSON document |
| 17 |
|
| 18 |
# Supported Features |
| 19 |
|
| 20 |
#### The basic XPath patterns. |
| 21 |
|
| 22 |
> The basic XPath patterns cover 90% of the cases that most stylesheets will need. |
| 23 |
|
| 24 |
- `node` : Selects all child elements with nodeName of node. |
| 25 |
|
| 26 |
- `*` : Selects all child elements. |
| 27 |
|
| 28 |
- `@attr` : Selects the attribute attr. |
| 29 |
|
| 30 |
- `@*` : Selects all attributes. |
| 31 |
|
| 32 |
- `node()` : Matches an org.w3c.dom.Node. |
| 33 |
|
| 34 |
- `text()` : Matches a org.w3c.dom.Text node. |
| 35 |
|
| 36 |
- `comment()` : Matches a comment. |
| 37 |
|
| 38 |
- `.` : Selects the current node. |
| 39 |
|
| 40 |
- `..` : Selects the parent of current node. |
| 41 |
|
| 42 |
- `/` : Selects the document node. |
| 43 |
|
| 44 |
- `a[expr]` : Select only those nodes matching a which also satisfy the expression expr. |
| 45 |
|
| 46 |
- `a[n]` : Selects the nth matching node matching a When a filter's expression is a number, XPath selects based on position. |
| 47 |
|
| 48 |
- `a/b` : For each node matching a, add the nodes matching b to the result. |
| 49 |
|
| 50 |
- `a//b` : For each node matching a, add the descendant nodes matching b to the result. |
| 51 |
|
| 52 |
- `//b` : Returns elements in the entire document matching b. |
| 53 |
|
| 54 |
- `a|b` : All nodes matching a or b, union operation(not boolean or). |
| 55 |
|
| 56 |
- `(a, b, c)` : Evaluates each of its operands and concatenates the resulting sequences, in order, into a single result sequence |
| 57 |
|
| 58 |
- `(a/b)` : Selects all matches nodes as grouping set. |
| 59 |
|
| 60 |
#### Node Axes |
| 61 |
|
| 62 |
- `child::*` : The child axis selects children of the current node. |
| 63 |
|
| 64 |
- `child::node()`: Selects all the children of the context node. |
| 65 |
- `child::text()`: Selects all text node children of the context node. |
| 66 |
|
| 67 |
- `descendant::*` : The descendant axis selects descendants of the current node. It is equivalent to '//'. |
| 68 |
|
| 69 |
- `descendant-or-self::*` : Selects descendants including the current node. |
| 70 |
|
| 71 |
- `attribute::*` : Selects attributes of the current element. It is equivalent to @\* |
| 72 |
|
| 73 |
- `following-sibling::*` : Selects nodes after the current node. |
| 74 |
|
| 75 |
- `preceding-sibling::*` : Selects nodes before the current node. |
| 76 |
|
| 77 |
- `following::*` : Selects the first matching node following in document order, excluding descendants. |
| 78 |
|
| 79 |
- `preceding::*` : Selects the first matching node preceding in document order, excluding ancestors. |
| 80 |
|
| 81 |
- `parent::*` : Selects the parent if it matches. The '..' pattern from the core is equivalent to 'parent::node()'. |
| 82 |
|
| 83 |
- `ancestor::*` : Selects matching ancestors. |
| 84 |
|
| 85 |
- `ancestor-or-self::*` : Selects ancestors including the current node. |
| 86 |
|
| 87 |
- `self::*` : Selects the current node. '.' is equivalent to 'self::node()'. |
| 88 |
|
| 89 |
#### Expressions |
| 90 |
|
| 91 |
The gxpath supported three types: number, boolean, string. |
| 92 |
|
| 93 |
- `path` : Selects nodes based on the path. |
| 94 |
|
| 95 |
- `a = b` : Standard comparisons. |
| 96 |
|
| 97 |
- `a = b` : True if a equals b. |
| 98 |
- `a != b` : True if a is not equal to b. |
| 99 |
- `a < b` : True if a is less than b. |
| 100 |
- `a <= b` : True if a is less than or equal to b. |
| 101 |
- `a > b` : True if a is greater than b. |
| 102 |
- `a >= b` : True if a is greater than or equal to b. |
| 103 |
|
| 104 |
- `a + b` : Arithmetic expressions. |
| 105 |
|
| 106 |
- `- a` Unary minus |
| 107 |
- `a + b` : Addition |
| 108 |
- `a - b` : Subtraction |
| 109 |
- `a * b` : Multiplication |
| 110 |
- `a div b` : Division |
| 111 |
- `a mod b` : Modulus (division remainder) |
| 112 |
|
| 113 |
- `a or b` : Boolean `or` operation. |
| 114 |
|
| 115 |
- `a and b` : Boolean `and` operation. |
| 116 |
|
| 117 |
- `(expr)` : Parenthesized expressions. |
| 118 |
|
| 119 |
- `fun(arg1, ..., argn)` : Function calls: |
| 120 |
|
| 121 |
| Function | Supported | |
| 122 |
| ----------------------- | --------- | |
| 123 |
| `boolean()` | ✓ | |
| 124 |
| `ceiling()` | ✓ | |
| 125 |
| `choose()` | ✗ | |
| 126 |
| `concat()` | ✓ | |
| 127 |
| `contains()` | ✓ | |
| 128 |
| `count()` | ✓ | |
| 129 |
| `current()` | ✗ | |
| 130 |
| `document()` | ✗ | |
| 131 |
| `element-available()` | ✗ | |
| 132 |
| `ends-with()` | ✓ | |
| 133 |
| `false()` | ✓ | |
| 134 |
| `floor()` | ✓ | |
| 135 |
| `format-number()` | ✗ | |
| 136 |
| `function-available()` | ✗ | |
| 137 |
| `generate-id()` | ✗ | |
| 138 |
| `id()` | ✗ | |
| 139 |
| `key()` | ✗ | |
| 140 |
| `lang()` | ✗ | |
| 141 |
| `last()` | ✓ | |
| 142 |
| `local-name()` | ✓ | |
| 143 |
| `lower-case()`[^1] | ✓ | |
| 144 |
| `matches()` | ✓ | |
| 145 |
| `name()` | ✓ | |
| 146 |
| `namespace-uri()` | ✓ | |
| 147 |
| `normalize-space()` | ✓ | |
| 148 |
| `not()` | ✓ | |
| 149 |
| `number()` | ✓ | |
| 150 |
| `position()` | ✓ | |
| 151 |
| `replace()` | ✓ | |
| 152 |
| `reverse()` | ✓ | |
| 153 |
| `round()` | ✓ | |
| 154 |
| `starts-with()` | ✓ | |
| 155 |
| `string()` | ✓ | |
| 156 |
| `string-join()`[^1] | ✓ | |
| 157 |
| `string-length()` | ✓ | |
| 158 |
| `substring()` | ✓ | |
| 159 |
| `substring-after()` | ✓ | |
| 160 |
| `substring-before()` | ✓ | |
| 161 |
| `sum()` | ✓ | |
| 162 |
| `system-property()` | ✗ | |
| 163 |
| `translate()` | ✓ | |
| 164 |
| `true()` | ✓ | |
| 165 |
| `unparsed-entity-url()` | ✗ | |
| 166 |
|
| 167 |
[^1]: XPath-2.0 expression |