[feature] XPath queries should support multiple handlers by using namespaces #2370
Replies: 1 comment 1 reply
-
|
Apologies for not responding to this issue for so many years. Without a specific use case, I am guessing a bit, but I think what you're asking for is an interface something like: #! /usr/bin/env ruby
require "nokogiri"
xml = <<~EOF
<root>
<message>a</message>
<message>b</message>
</root>
EOF
handler_uri = "http://example.com/handler"
handler = Class.new do
def contents(x)
x.map(&:content).join(",")
end
end.new
upcase_uri = "http://example.com/upcase"
upcase_handler = Class.new do
def contents(x)
x.map(&:content).map(&:upcase).join(",")
end
end.new
handlers = {handler_uri => handler, upcase_uri => upcase_handler}
namespaces = {handler: handler_uri, upcase: upcase_uri}
doc = Nokogiri::XML::Document.parse(xml)
doc.xpath("handler:contents(//message)", namespaces, handlers) == "a,b" or raise("no")
doc.xpath("upcase:contents(//message)", namespaces, handlers) == "A,B" or raise("no")That is: the hash-of-handlers would have URI keys that could be used to scope the xpath function lookup. Is that about right? If so, the way we'd have to do this is introduce keyword arguments to eliminate ambiguity between the namespaces hash and the handlers hash. I'm not overly opposed to doing this, but it does feel like the easy workaround is to create a custom handler class with all your custom functions defined on it. Can you help me understand your use case more deeply? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
XPathContext#evaluteuse thexmlXPathRegisterFuncLookupfunction to register a lookup function, unfortunately namespace is not used during the lookup phase.To fix it:
Node#extract_paramsshould allow a hash as handler (as forXSLT#parse)lookupshould use thens_uriparameterBeta Was this translation helpful? Give feedback.
All reactions