Skip to content

Loose and strict XPath variables/namespaces #349

Description

@tompng

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions