Loose and strict XPath variables/namespaces
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 182
- Forks
- 99
- Avg merge
- 2d 23h
- Merged PRs (30d)
- 8
Description
Loose keys
XPath namespaces and variables doesn't accept symbol keys. It'll be convenient if symbol keys are accepted.
Nokogiri accepts both string and symbol keys.
namespaces = { ns: 'url' }
variables = { id: 'a' }
xml = '<foo xmlns="url"><bar id="a"/></foo>'
Nokogiri::XML.parse(xml).xpath('//ns:bar[@id=$id]', namespaces, variables)
Strict value check
XPath variables have value validation. Namespaces can also have validation
Nokogiri::XML.parse('<a/>').xpath('//a', { ns: 42 }) #=> TypeError
Strict variable existence check
There's two options
- Reference time check
- Pros: easy to implement
- Cons: error is document dependent
- Pre-check all variables in xpath
- Pros: can detect variable name typo easily
- discussed in https://github.com/ruby/rexml/pull/342#discussion_r3586024018
Nokogiri(libxml2) implements reference time check.
Nokogiri::XML.parse('<b/>').xpath('//a[@id=$foo]', nil, {fooo: '1'}) # => []
Nokogiri::XML.parse('<a/>').xpath('//a[@id=$foo]', nil, {fooo: '1'}) # => ERROR: Undefined variable
XPath 1.0 spec:
A VariableReference evaluates to the value to which the variable name is bound in the set of variable bindings in the context. It is an error if the variable name is not bound to any value in the set of variable bindings in the expression context.
I'm not sure whether it should be an error in evaluation context (reference-time) or in pre-check phase.
In XPath 2.0, it's explicitly defined to be a static-error, so XPath 1.0 spec might be ambiguous.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at REXML's XPath entry point and trace how namespace and variable bindings are handled. Compare the requested symbol-key and namespace-value behavior with the Nokogiri examples and the XPath 1.0 specification. Decide whether undefined variables are checked during reference-time evaluation or before evaluation, then cover the chosen behavior with tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100