[lxml-dev] space in attribute name: xpath expression?

TP paratribulations at free.fr
Tue Mar 17 11:13:17 CET 2009


Hi everybody,

It seems not possible to define with fromstring() or ET.XML a tree
containing attributes with spaces.
But it is possible by adding the attribute containing a space afterwards,
see the example below.

###################
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import lxml.etree as ET

root = ET.XML("<root><foo attri='bar'>data</foo></root>")
foo_elem = root.xpath( "//foo" )
foo_elem[0].set( "tu tu", "22" )
print ET.tostring( root )
###################

We obtain:
<root><foo attri="bar" tu tu="22">data</foo></root>

It seems a bad idea to have spaces in attributes. I have not found a way to
make an xpath request work, for example the two following ones yield an
error:

print root.xpath( "//*[@tu tu=22]" )
print root.xpath( "//*[@tu\ tu=22]" )

>From another point of view, often we would like to define attribute names as
they are, i.e. english expressions with spaces. How do you proceed? Put
underscores in the attribute names, and then remove them when displaying in
the tree (for example in a graphical widget)? Or define the correspondance
between the attribute names and the english names in some part of the XML
file (for example, the attribute names could be tags, associated to some
text that would contain the english names.

Thanks

-- 
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
9&1+,\'Z4(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)



More information about the lxml-dev mailing list