Description
I've found myself wanting to write XPath expressions equivalent or similar to the CSS E:first-child
selector. The two use cases I have so far is:
- first child element is a given element --
child::*[1]/self::element-name
; - first child node is a text node --
child::node()[1]/self::text()
.
These are harder to read in XPath as they are spanning multiple steps, so having shorthand selectors for these could be useful.
NOTE: I don't have any concrete proposals for this.
Using first-child::element-name
and first-child::text()
would be easier to read. The former is CSS-like (match elements only), while the latter is XPath-like (match any nodes). Using different context behaviour would be confusing, and inconsistent with the other XPath axis selectors. It would also prevent supporting a child::node()[1]/self::element-name
equivalent.
Another possibility would be to create two separate axes, such as first-child
and first-child-element
. This would be consistent with axes like ancestor
/ancestor-or-self
, but could quickly increase the number of axes. This version would add at least 4 new axes:
first-child::
(forward);first-child-element::
(forward);last-child::
(reverse);last-child-element::
(reverse).
NOTE: Only the tree traversal CSS selectors would be applicable. CSS selectors like E:hover
require context information that is not available, nor relevant to XPath.