[XML-SIG] cloneNode doesn't copy attributes?

Jan Nieuwenhuizen janneke@gnu.org
14 Jan 2002 23:25:22 +0100


Hi,

It seems that cloneNode in python-xml 0.7 (latest Debian unstable
python2.2-xml) doesn't copy attributes.  Should it?  What am I doing
wrong/what doco should I read?  See below.

I've made several attempts to write something as simple as this, but
it seems not a trivial to determine which examples are obsolete, and
which are preferred now (xml.dom.html_builder.HtmlBuilder and
HtmlWriter are too old, pyexpat is too new/not for plain HTML, SAX is
simple, but I don't want simple, rather standard and java-like, but in
python, right? -- dazzle).

Greetings,
Jan.



#!/usr/bin/python2.2

from xml.dom.ext.reader import HtmlLib
from xml.dom.ext import XHtmlPrettyPrint
#from xml.dom.ext import Print
import sys

in_html = '''
<html>
<body id='body'>
<h1 foo="bar">baz</h1>
</body>
</html>
'''

out_html = '''
<html>
<foo></foo>
</html>
'''

in_doc = HtmlLib.FromHtml (in_html)
out_doc = HtmlLib.FromHtml (out_html)
#Print (in_doc, stream=sys.stdout)
XHtmlPrettyPrint (in_doc, stream=sys.stdout)
print


n = in_doc._get_body ()

print `n`
print
m = out_doc.importNode (n.cloneNode (1), 1)

print `m`
print
print `out_doc.documentElement.firstChild`
out_doc.documentElement.replaceChild (m, out_doc.documentElement.firstChild)
print
# Print (out_doc, stream=sys.stdout)
XHtmlPrettyPrint (out_doc, stream=sys.stdout)

-- 
Jan Nieuwenhuizen <janneke@gnu.org> | GNU LilyPond - The music typesetter
http://www.xs4all.nl/~jantien       | http://www.lilypond.org