xpath2
Version, currently 0.2.05 versions
github.com/naqvis/crystal-xpath2
Pure Crystal XPath 1.0/2.0 engine for selecting and evaluating nodes from HTML/XML documents.
Installation
# Add this to your shard.yml
dependencies:
xpath2:
github: naqvis/crystal-xpath2
version: ~> 0.2.0Then run:
shards installshard.yml
- Crystal
~> 1.0- License
- MIT
- Author
- Ali Naqvi
Dependencies
This version declares no dependencies.
README
Crystal XPath2
Crystal XPath2 Shard provide XPath implementation in Pure Crystal. Performs the compilation of XPath expression and provides mechanism to select/evaluate nodes from HTML or other documents using XPath expression
Supported Features
The basic XPath patterns.
The basic XPath patterns cover 90% of the cases that most stylesheets will need.
-
node: Selects all child elements with node Name of node. -
*: Selects all child elements. -
@attr: Selects the attribute attr. -
@*: Selects all attributes. -
node(): Matches an org.w3c.dom.Node. -
text(): Matches a org.w3c.dom.Text node. -
comment(): Matches a comment. -
.: Selects the current node. -
..: Selects the parent of current node. -
/: Selects the document node. -
a[expr]: Select only those nodes matching a which also satisfy the expression expr. -
a[n]: Selects the nth matching node matching a When a filter's expression is a number, XPath selects based on position. -
a/b: For each node matching a, add the nodes matching b to the result. -
a//b: For each node matching a, add the descendant nodes matching b to the result. -
//b: Returns elements in the entire document matching b. -
a|b: All nodes matching a or b, union operation(not boolean or). -
(a, b, c): Evaluates each of its operands and concatenates the resulting sequences, in order, into a single result sequence
Node Axes
-
child::*: The child axis selects children of the current node. -
descendant::*: The descendant axis selects descendants of the current node. It is equivalent to"//". -
descendant-or-self::*: Selects descendants including the current node. -
attribute::*: Selects attributes of the current element. It is equivalent to@* -
following-sibling::*: Selects nodes after the current node. -
preceding-sibling::*: Selects nodes before the current node. -
following::*: Selects the first matching node following in document order, excluding descendants. -
preceding::*: Selects the first matching node preceding in document order, excluding ancestors. -
parent::*: Selects the parent if it matches. The".."pattern from the core is equivalent to 'parent::node()'. -
ancestor::*: Selects matching ancestors. -
ancestor-or-self::*: Selects ancestors including the current node. -
self::*: Selects the current node.'.'is equivalent to"self::node()".
Expressions
Shard supports three types: number, boolean, string.
-
path: Selects nodes based on the path. -
a = b: Standard comparisons.- a
=btrueif a equals b. - a
!=btrueif a is not equal to b. - a
<btrueif a is less than b. - a
<=btrueif a is less than or equal to b. - a
>btrueif a is greater than b. - a
>=btrueif a is greater than or equal to b.
- a
-
a + b: Arithmetic expressions.- aUnary minus- a
+b Add - a
-b Substract - a
*b Multiply - a
divb Divide - a
modb Floating point mod, like Java.
-
a or b: Booleanoroperation. -
a and b: Booleanandoperation. -
(expr): Parenthesized expressions. -
fun(arg1, ..., argn): Function calls: -
$variable: Variable references. Variables are bound at compile time viaXPath2.compile(expr, variables).
| Function | Supported |
|---|---|
abs() | ✓ |
boolean() | ✓ |
ceiling() | ✓ |
compare() | ✓ |
concat() | ✓ |
contains() | ✓ |
count() | ✓ |
distinct-values() | ✓ |
empty() | ✓ |
ends-with() | ✓ |
exists() | ✓ |
false() | ✓ |
floor() | ✓ |
function-available() | ✓ |
generate-id() | ✓ |
id() | ✓ |
index-of() | ✓ |
insert-before() | ✓ |
lang() | ✓ |
last() | ✓ |
local-name() | ✓ |
lower-case() | ✓ |
matches() | ✓ |
name() | ✓ |
namespace-uri() | ✓ |
normalize-space() | ✓ |
not() | ✓ |
number() | ✓ |
position() | ✓ |
remove() | ✓ |
replace() | ✓ |
reverse() | ✓ |
round() | ✓ |
starts-with() | ✓ |
string() | ✓ |
string-join() | ✓ |
string-length() | ✓ |
subsequence() | ✓ |
substring() | ✓ |
substring-after() | ✓ |
substring-before() | ✓ |
sum() | ✓ |
tokenize() | ✓ |
translate() | ✓ |
true() | ✓ |
upper-case() | ✓ |
Note: XSLT-specific functions (
choose,current,document,element-available,format-number,key,system-property,unparsed-entity-url) are not supported as they require an XSLT processing context.
Installation
-
Add the dependency to your
shard.yml:dependencies: xpath2: github: naqvis/crystal-xpath2 -
Run
shards install
Usage
refer to spec for usage examples refer to spec or refer to Crystal HTML5 and JSON XPath for implementation details.
Development
To run all tests:
crystal spec
Contributing
- Fork it (https://github.com/naqvis/crystal-xpath2/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Contributors
- Ali Naqvi - creator and maintainer
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
0.2.0- Tagged
- Apr 16, 2026
- Commit
8062235829e7- Crystal
~> 1.0- Indexed
- yes
Dependents
Repository
github.com/naqvis/crystal-xpath2
Metadata
- Created
- Aug 12, 2026
- Updated
- Nov 10, 2026
- Synced
- Nov 9, 2026
- Versions
- 5