'Serialize this L{Element} to the given stream.
@param stream: A file-like object to which this L{Element} will be
written.
@param nsprefixes: A C{dict} mapping namespace URIs as C{str} to
prefixes as C{str}.  This defines the prefixes which are already in
scope in the document at the point at which this L{Element} exists.
This is essentially an implementation detail for namespace support.
Applications should not try to use it.
@param namespace: The namespace URI as a C{str} which is the default at
the point in the document at which this L{Element} exists.  This is
essentially an implementation detail for namespace support.
Applications should not try to use it.'

def writexml(self, stream, indent='', addindent='', newl='', strip=0, nsprefixes={}, namespace=''):

    ALLOWSINGLETON = ('img', 'br', 'hr', 'base', 'meta', 'link', 'param', 'area', 'input', 'col', 'basefont', 'isindex', 'frame')
    BLOCKELEMENTS = ('html', 'head', 'body', 'noscript', 'ins', 'del', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'script', 'ul', 'ol', 'dl', 'pre', 'hr', 'blockquote', 'address', 'p', 'div', 'fieldset', 'table', 'tr', 'form', 'object', 'fieldset', 'applet', 'map')
    FORMATNICELY = ('tr', 'ul', 'ol', 'head')
    if (not self.preserveCase):
        self.endTagName = self.tagName
    w = _streamWriteWrapper(stream)
    if self.nsprefixes:
        newprefixes = self.nsprefixes.copy()
        for ns in nsprefixes.keys():
            if (ns in newprefixes):
                del newprefixes[ns]
    else:
        newprefixes = {}
    begin = ['<']
    if (self.tagName in BLOCKELEMENTS):
        begin = ([newl, indent] + begin)
    bext = begin.extend
    writeattr = (lambda _atr, _val: bext(('    ', _atr, '="', escape(_val), '"')))
    endTagName = self.endTagName
    if ((namespace != self.namespace) and (self.namespace is not None)):
        if (self.namespace in nsprefixes):
            prefix = nsprefixes[self.namespace]
            bext(((prefix + ':') + self.tagName))
            endTagName = ((prefix + ':') + self.endTagName)
        else:
            bext(self.tagName)
            writeattr('xmlns', self.namespace)
            namespace = self.namespace
    else:
        bext(self.tagName)
    j = ''.join
    for (attr, val) in sorted(self.attributes.items()):
        if isinstance(attr, tuple):
            (ns, key) = attr
            if (ns in nsprefixes):
                prefix = nsprefixes[ns]
            else:
                prefix = next(genprefix)
                newprefixes[ns] = prefix
            assert (val is not None)
            writeattr(((prefix + ':') + key), val)
        else:
            assert (val is not None)
            writeattr(attr, val)
    if newprefixes:
        for (ns, prefix) in iteritems(newprefixes):
            if prefix:
                writeattr(('xmlns:' + prefix), ns)
        newprefixes.update(nsprefixes)
        downprefixes = newprefixes
    else:
        downprefixes = nsprefixes
    w(j(begin))
    if self.childNodes:
        w('>')
        newindent = (indent + addindent)
        for child in self.childNodes:
            if ((self.tagName in BLOCKELEMENTS) and (self.tagName in FORMATNICELY)):
                w(j((newl, newindent)))
            child.writexml(stream, newindent, addindent, newl, strip, downprefixes, namespace)
        if (self.tagName in BLOCKELEMENTS):
            w(j((newl, indent)))
        w(j(('</', endTagName, '>')))
    elif (self.tagName.lower() not in ALLOWSINGLETON):
        w(j(('></', endTagName, '>')))
    else:
        w('    />')
