diff --git a/ext/java/nokogiri/XmlXpathContext.java b/ext/java/nokogiri/XmlXpathContext.java index 40599232e42..5996956f68f 100644 --- a/ext/java/nokogiri/XmlXpathContext.java +++ b/ext/java/nokogiri/XmlXpathContext.java @@ -154,7 +154,11 @@ public class XmlXpathContext extends RubyObject public IRubyObject register_ns(IRubyObject prefix, IRubyObject uri) { - nsContext.registerNamespace(prefix.asJavaString(), uri.asJavaString()); + if (uri.isNil()) { + nsContext.deregisterNamespace(prefix.asJavaString()); + } else { + nsContext.registerNamespace(prefix.asJavaString(), uri.asJavaString()); + } return this; } @@ -169,10 +173,23 @@ public class XmlXpathContext extends RubyObject variableResolver = NokogiriXPathVariableResolver.create(); this.variableResolver = variableResolver; } - variableResolver.registerVariable(name.asJavaString(), value.asJavaString()); + if (value.isNil()) { + variableResolver.deregisterVariable(name.asJavaString()); + } else { + variableResolver.registerVariable(name.asJavaString(), value.asJavaString()); + } return this; } + @JRubyMethod(name = "node=") + public IRubyObject + set_node(ThreadContext context, IRubyObject rb_node) + { + this.context = (XmlNode) rb_node; + return rb_node; + } + + private IRubyObject node_set(ThreadContext context, String expr, IRubyObject handler) { diff --git a/ext/java/nokogiri/internals/NokogiriNamespaceContext.java b/ext/java/nokogiri/internals/NokogiriNamespaceContext.java index 24381a4c8d0..0ff2920ef40 100644 --- a/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +++ b/ext/java/nokogiri/internals/NokogiriNamespaceContext.java @@ -107,4 +107,10 @@ public final class NokogiriNamespaceContext implements NamespaceContext register.put(prefix, uri); } + public void + deregisterNamespace(String prefix) + { + if ("xmlns".equals(prefix)) { prefix = ""; } + register.remove(prefix); + } } diff --git a/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java b/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java index 0c8ca6d29b1..e830f4c6c94 100644 --- a/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +++ b/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java @@ -34,4 +34,10 @@ public class NokogiriXPathVariableResolver implements XPathVariableResolver { variables.put(new QName(name), value); } + public void + deregisterVariable(String name) + { + variables.remove(new QName(name)); + } + } diff --git a/test/xml/test_xpath_context.rb b/test/xml/test_xpath_context.rb index aa1d88a55c2..47390cdf1c0 100644 --- a/test/xml/test_xpath_context.rb +++ b/test/xml/test_xpath_context.rb @@ -16,19 +16,19 @@ module XML xc = XPathContext.new(doc) assert_raises(XPath::SyntaxError) do - xc.evaluate("//xmlns:child") + xc.evaluate("//foo:child") end - xc.register_namespaces({ "xmlns" => "http://nokogiri.org/default" }) + xc.register_namespaces({ "foo" => "http://nokogiri.org/default" }) assert_pattern do - xc.evaluate("//xmlns:child") => [ + xc.evaluate("//foo:child") => [ { name: "child", namespace: { href: "http://nokogiri.org/default" } } ] end - xc.register_namespaces({ "xmlns" => nil }) + xc.register_namespaces({ "foo" => nil }) assert_raises(XPath::SyntaxError) do - xc.evaluate("//xmlns:child") + xc.evaluate("//foo:child") end end