mimidom: setting a value of an attribute

Martin v. Loewis martin at v.loewis.de
Thu Aug 1 23:21:43 CEST 2002


Michael Grabietz <michael.grabietz at img-online.de> writes:

> # create an attribute
> a1 = doc.createAttributeNS(None,u'attrib')

This is a namespace operation.

> 
> # add it under the sect1 element
> s1.setAttributeNode(a1)
> 
> # Now I want to set the value 'attvalue' to the previously added
> attribute a1
> # The following fails !
> s1.setAttribute(a1,'attvalue')

This is not. This is the first error - you should not mix NS and
non-NS operations (they do interoperate to some level, but you really
need to decide whether you want to make a namespace-aware or a
namespace-unaware application).

The real problem, however, is that you misunderstand the signature of
setAttribute: it expects the attribute name and attribute value, see

http://www.python.org/doc/lib/dom-element-objects.html

So proper usage would be (leaving namespaces aside):

s1.setAttribute('attrib', 'attvalue')

That saves you creating the attribut node explicitly. If you want to
use the attribute node, you should do

a1.nodeValue = 'attvalue'

HTH,
Martin



More information about the Python-list mailing list