ruby / ruby/rexml

Loose and strict XPath variables/namespaces

Open
#349 0 comments 0 reactions 0 assignees View on GitHub

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

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.