diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb
index 422a043..66ed881 100644
--- a/lib/oga/xpath/evaluator.rb
+++ b/lib/oga/xpath/evaluator.rb
@@ -706,21 +706,33 @@ module Oga
# @return [Oga::XML::NodeSet]
#
def on_call_local_name(context, expression = nil)
- if expression
- node = process(expression, context)
-
- if node.is_a?(XML::NodeSet)
- node = node.first
- else
- raise TypeError, 'local-name() only takes node sets as arguments'
- end
- else
- node = current_node
- end
+ node = context_node(context, expression)
return node.is_a?(XML::Element) ? node.name : ''
end
+ ##
+ # Processes the `namespace-uri()` function call.
+ #
+ # This function call returns the namespace URI of one of the following:
+ #
+ # * The current context node (if any)
+ # * The first node in the supplied node set
+ #
+ # @param [Oga::XML::NodeSet] context
+ # @param [Oga::XPath::Node] expression
+ # @return [Oga::XML::NodeSet]
+ #
+ def on_call_namespace_uri(context, expression = nil)
+ node = context_node(context, expression)
+
+ if node.is_a?(XML::Element) and node.namespace
+ return node.namespace.uri
+ else
+ return ''
+ end
+ end
+
##
# Processes an `(int)` node. This method simply returns the value as a
# Float.
@@ -744,6 +756,30 @@ module Oga
return ast_node.children[0]
end
+ ##
+ # Returns the node for a function call. This node is either the first node
+ # in the supplied node set, or the first node in the current context.
+ #
+ # @param [Oga::XML::NodeSet] context
+ # @param [Oga::XPath::Node] expression
+ # @return [Oga::XML::Node]
+ #
+ def context_node(context, expression = nil)
+ if expression
+ node = process(expression, context)
+
+ if node.is_a?(XML::NodeSet)
+ node = node.first
+ else
+ raise TypeError, 'only node sets can be used as arguments'
+ end
+ else
+ node = current_node
+ end
+
+ return node
+ end
+
##
# Returns a node set containing all the child nodes of the given set of
# nodes.
diff --git a/spec/oga/xpath/evaluator/calls/namespace_uri_spec.rb b/spec/oga/xpath/evaluator/calls/namespace_uri_spec.rb
new file mode 100644
index 0000000..54e752b
--- /dev/null
+++ b/spec/oga/xpath/evaluator/calls/namespace_uri_spec.rb
@@ -0,0 +1,38 @@
+require 'spec_helper'
+
+describe Oga::XPath::Evaluator do
+ context 'namespace-uri() function' do
+ before do
+ @document = parse('')
+ @evaluator = described_class.new(@document)
+ end
+
+ context 'outside predicates' do
+ example 'return the namespace URI of the node' do
+ @evaluator.evaluate('namespace-uri(root/x:a)').should == 'y'
+ end
+
+ example 'return an empty string when there is no namespace URI' do
+ @evaluator.evaluate('namespace-uri(root/b)').should == ''
+ end
+
+ example 'raise TypeError for invalid argument types' do
+ block = lambda { @evaluator.evaluate('namespace-uri("foo")') }
+
+ block.should raise_error(TypeError)
+ end
+ end
+
+ context 'inside predicates' do
+ before do
+ @set = @evaluator.evaluate('root/*[namespace-uri()]')
+ end
+
+ it_behaves_like :node_set, :length => 1
+
+ example 'return the node' do
+ @set[0].should == @document.children[0].children[0]
+ end
+ end
+ end
+end