diff --git a/lib/rexml/parsers/sax2parser.rb b/lib/rexml/parsers/sax2parser.rb
index 5452d4b8..a51477de 100644
--- a/lib/rexml/parsers/sax2parser.rb
+++ b/lib/rexml/parsers/sax2parser.rb
@@ -259,6 +259,8 @@ def add( pair )
end
def get_namespace( prefix )
+ return nil if @namespace_stack.empty?
+
uris = (@namespace_stack.find_all { |ns| not ns[prefix].nil? }) ||
(@namespace_stack.find { |ns| not ns[nil].nil? })
uris[-1][prefix] unless uris.nil? or 0 == uris.size
diff --git a/test/test_sax.rb b/test/test_sax.rb
index 6aaeb618..caec983b 100644
--- a/test/test_sax.rb
+++ b/test/test_sax.rb
@@ -99,6 +99,52 @@ def test_sax2
end
end
+ def test_without_namespace
+ xml = <<-XML
+
+
+
+
+
+ XML
+
+ parser = REXML::Parsers::SAX2Parser.new(xml)
+ elements = []
+ parser.listen(:start_element) do |uri, localname, qname, attrs|
+ elements << [uri, localname, qname, attrs]
+ end
+ parser.parse
+ assert_equal([
+ [nil, "root", "root", {}],
+ [nil, "a", "a", {"att1"=>"1", "att2"=>"2", "att3"=>"<"}],
+ [nil, "b", "b", {}]
+ ], elements)
+ end
+
+ def test_with_namespace
+ xml = <<-XML
+
+
+
+
+
+ XML
+
+ parser = REXML::Parsers::SAX2Parser.new(xml)
+ elements = []
+ parser.listen(:start_element) do |uri, localname, qname, attrs|
+ elements << [uri, localname, qname, attrs]
+ end
+ parser.parse
+ assert_equal([
+ ["http://example.org/default", "root", "root", {"xmlns"=>"http://example.org/default", "xmlns:bar"=>"http://example.org/bar", "xmlns:foo"=>"http://example.org/foo"}],
+ ["http://example.org/default", "a", "a", {"att"=>"<", "bar:att"=>"2", "foo:att"=>"1"}],
+ ["http://example.org/bar", "b", "bar:b", {}]
+ ], elements)
+ end
+
class EntityExpansionLimitTest < Test::Unit::TestCase
class GeneralEntityTest < self
def test_have_value