Next / Previous / Contents / TCC Help System / NM Tech homepage

14.13. Element.update(): Copy XML attributes to the element

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.

xml4create.py
# - - -   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:

All this method has to do is iterate over the elements of the given dictionary, and call self.__setAttr() for each one.

xml4create.py
        #-- 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] )