[lxml-dev] xmlns / xmlns:xmlns inconsistency

Aaron Brady castironpi at gmail.com
Fri Sep 12 02:08:22 CEST 2008


Hello,

I don't know if anyone checks this or cares.

I tried to set a 'xmlns' attribute of a node.  'etree.tostring'
produced an attribute name of 'xmlns:xmlns' instead.  I found a
workaround: call 'root.set( 'xmlns',
'urn:schemas-microsoft-com:office:spreadsheet' )' after creating the
node.  Full report, thanks for your time.

from lxml import etree

print "lxml.etree:       ", etree.LXML_VERSION
print "libxml used:      ", etree.LIBXML_VERSION
print "libxml compiled:  ", etree.LIBXML_COMPILED_VERSION
print "libxslt used:     ", etree.LIBXSLT_VERSION
print "libxslt compiled: ", etree.LIBXSLT_COMPILED_VERSION

'''
lxml.etree:        (2, 1, 0, 0)
libxml used:       (2, 6, 32)
libxml compiled:   (2, 6, 32)
libxslt used:      (1, 1, 23)
libxslt compiled:  (1, 1, 23)
'''

'''
Description:

Setting 'xmlns' attribute of tag produces 'xmlns:xmlns' instead.
'''

'''
Possibly related issue:

* With ``lxml.doctestcompare`` if you do ``<tag xmlns="...">`` in your
  output, it will then be namespace-neutral (before the ellipsis was
  treated as a real namespace).
'''

'''
Target:

<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
---------------^-----------
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">

Current:
<Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns:xmlns="urn:schemas-microsoft-com:office:spreadsheet"
----------^-----------
    xmlns:html="http://www.w3.org/TR/REC-html40"
    xmlns:o="urn:schemas-microsoft-com:office:office">
'''

nns= { 'xmlns': 'urn:schemas-microsoft-com:office:spreadsheet',
        'o': 'urn:schemas-microsoft-com:office:office',
        'x': 'urn:schemas-microsoft-com:office:excel',
        'ss': 'urn:schemas-microsoft-com:office:spreadsheet',
        'html': 'http://www.w3.org/TR/REC-html40' }
root= etree.Element( 'Workbook', nsmap= nns )
out= etree.tostring( root, pretty_print= True, xml_declaration=True )
print( out )


'''
Workaround:
'''
nns= { 'o': 'urn:schemas-microsoft-com:office:office',
        'x': 'urn:schemas-microsoft-com:office:excel',
        'ss': 'urn:schemas-microsoft-com:office:spreadsheet',
        'html': 'http://www.w3.org/TR/REC-html40' }
root= etree.Element( 'Workbook', nsmap= nns )
root.set( 'xmlns', 'urn:schemas-microsoft-com:office:spreadsheet' )
out= etree.tostring( root, pretty_print= True, xml_declaration=True )
print( out )

'''
Output:
<?xml version='1.0' encoding='ASCII'?>
<Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns:html="http://www.w3.org/TR/REC-html40"
    xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns="urn:schemas-microsoft-com:office:spreadsheet"/>
---------^-------
'''


More information about the lxml-dev mailing list