The attrs argument is a dictionary
containing XML attribute names and values. The purpose
of this method is to set up those attributes on the DOM
Element node.
# - - - E l e m e n t . u p d a t e - - -
def update ( self, attrs ):
"""Add supplied attributes to the XML Element.
[ (self.doc is the containing Document) and
(attrs is a dictionary ->
if attrs contains any namespace prefixes that are
not keys in self.doc.nsMap ->
raise KeyError
else ->
self.node := self.node with attribute names
(with trailing "_" removed if present) and
corresponding values from attrs ]
"""
There are two minor complications:
The attribute name may contain a namespace prefix.
We must translate such prefixes into namespace URIs
using the .nsMap in the containing
Document. This can fail with a KeyError if the attribute's prefix is not in
that map.
If the attribute name ends with an underbar
(“_”), we remove it.
This allows creation of XML attributes whose names
are Python reserved words, e.g., class_ becomes class.
All this method has to do is iterate over the elements of
the given dictionary, and call self.__setAttr() for each one.
#-- 1 --
for attrName in attrs:
#-- 1 body --
# [ attrName is a key in attrs ->
# if attrName has a namespace prefix not defined
# in self.doc.nsMap ->
# raise KeyError
# else if attrName has a namespace prefix defined
# in self.doc.nsMap ->
# self.node := self.node with a new Attribute
# added having nsURI=self.doc.nsMap[attrName],
# localName=(attrName past the first
# colon, trailing underbar dropped if any),
# and value=attrs[attrName]
# else ->
# self.node := self.node with a new Attribute
# added having nsURI=None and
# localName=(attrName, trailing underbar
# dropped if any), and value=attrs[attrName] ]
self.__setAttr ( attrName, attrs[attrName] )