Here's my crack at it, using a Python SAX filter.
import sys, string
from xml.sax import saxutils, handler, make_parser
firstElement = True
class ContentGenerator(handler.ContentHandler):
def __init__(self, out = sys.stdout):
handler.ContentHandler.__init__(self)
self._out = out
def startDocument(self):
pass
def startElement(self, name, attrs):
global firstElement
if firstElement:
attrs = dict(attrs)
name = "z:" + name
if 'xmlns' in attrs:
attrs['xmlns:z'] = attrs['xmlns']
attrs['xmlns'] = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
firstElement = False
elif ':' not in name:
name = "z:" + name
self._out.write('<' + name)
for (name, value) in attrs.items():
self._out.write(' %s="%s"' % (name, saxutils.escape(value)))
self._out.write('>')
def endElement(self, name):
if ':' not in name:
name = "z:" + name
self._out.write('</%s>' % name)
def characters(self, content):
self._out.write(saxutils.escape(content))
def ignorableWhitespace(self, content):
self._out.write(content)
def processingInstruction(self, target, data):
self._out.write('<?%s %s?>' % (target, data))
parser = make_parser()
parser.setContentHandler(ContentGenerator())
parser.parse(sys.argv[1])
It jumps on the first element, mucks around with the attributes, and continues looking for all of the elements with the default namespace in the rest of the document. However, your comment that your documents have multiple xmlns="" attributes sprinkled around means this will need some help. The general technique isn't so bad, SAX pipelines are our friends :-).