Index: genshi/output.py
===================================================================
--- genshi/output.py	(revision 927)
+++ genshi/output.py	(working copy)
@@ -569,6 +569,7 @@
     def __call__(self, stream):
         prefixes = dict([(v, [k]) for k, v in self.prefixes.items()])
         namespaces = {XML_NAMESPACE.uri: ['xml']}
+        default = ['']
         def _push_ns(prefix, uri):
             namespaces.setdefault(uri, []).append(prefix)
             prefixes.setdefault(prefix, []).append(uri)
@@ -592,14 +593,14 @@
 
                 tagname = tag.localname
                 tagns = tag.namespace
-                if tagns:
+                if tagns and tagns != default[-1]:
                     if tagns in namespaces:
                         prefix = namespaces[tagns][-1]
                         if prefix:
                             tagname = u'%s:%s' % (prefix, tagname)
                     else:
                         _push_ns_attr((u'xmlns', tagns))
-                        _push_ns('', tagns)
+                        default.append(tagns)
 
                 new_attrs = []
                 for attr, value in attrs:
@@ -622,7 +623,7 @@
             elif kind is END:
                 tagname = data.localname
                 tagns = data.namespace
-                if tagns:
+                if tagns and tagns != default[-1]:
                     prefix = namespaces[tagns][-1]
                     if prefix:
                         tagname = u'%s:%s' % (prefix, tagname)
@@ -630,13 +631,21 @@
 
             elif kind is START_NS:
                 prefix, uri = data
-                if uri not in namespaces:
+                if prefix is '':
+                    if uri != default[-1]:
+                        _push_ns_attr(_make_ns_attr(prefix, uri))
+                    default.append(uri)
+                elif uri not in namespaces:
                     prefix = prefixes.get(uri, [prefix])[-1]
                     _push_ns_attr(_make_ns_attr(prefix, uri))
-                _push_ns(prefix, uri)
+                if prefix is not '':
+                    _push_ns(prefix, uri)
 
             elif kind is END_NS:
-                if data in prefixes:
+                uri = None
+                if data is '':
+                    uri = default.pop()
+                elif data in prefixes:
                     uris = prefixes.get(data)
                     uri = uris.pop()
                     if not uris:
@@ -646,10 +655,10 @@
                         uri_prefixes.pop()
                         if not uri_prefixes:
                             del namespaces[uri]
-                    if ns_attrs:
-                        attr = _make_ns_attr(data, uri)
-                        if attr in ns_attrs:
-                            ns_attrs.remove(attr)
+                if uri and ns_attrs:
+                    attr = _make_ns_attr(data, uri)
+                    if attr in ns_attrs:
+                        ns_attrs.remove(attr)
 
             else:
                 yield kind, data, pos
Index: genshi/template/tests/directives.py
===================================================================
--- genshi/template/tests/directives.py	(revision 927)
+++ genshi/template/tests/directives.py	(working copy)
@@ -847,7 +847,7 @@
         # FIXME: there should be a way to strip out unwanted/unused namespaces,
         #        such as the "x" in this example
         self.assertEqual("""<html xmlns:x="http://www.example.org/">
-          <div>Foo</div>
+          <div xmlns="http://www.example.org/">Foo</div>
         </html>""", str(tmpl.generate()))
 
     def test_match_with_position_predicate(self):

