Next / Previous / Contents / Shipman's homepage

27. Sox.leaf(): Write an element with no element children

sox.py
# - - -   S o x . l e a f

    def leaf(self, tag, *p, **kw):
        '''Write an element with no element children.
        '''

The argument processing is the same here as it is in Section 12, “Sox.start(): Start an element”: see Section 13, “Sox._nameCheck(): Is this a valid xml-name?” and Section 16, “Sox._sortParams(): Classify content and attribute arguments”.

sox.py
        #-- 1
        # [ if tag is an xml-name ->
        #     uTag  :=  tag as unicode
        #   else -> raise SoxError ]
        uTag = self._nameCheck(tag)

        #-- 2
        # [ if p and kw are valid ->
        #     contentList  :=  a list of unicode values made from
        #         the non-dictionary elements of p
        #     attribs  :=  a dictionary made from the dictionary
        #         elements of p, plus the elements of kw
        #   else -> raise SoxError ]
        contentList, attribs = self._sortParams(p, kw)

Because we know there will be no child elements, we can write the complete element right away. There are two cases, however. If there are no content elements, we can write an empty tag. If there is content, we write the start tag, then the content, then the end tag. See: Section 29, “Sox._emptyTag(): Write an empty XML tag”; Section 28, “Sox._startTag(): Write a start tag”; and Section 30, “Sox._endTag(): Write an XML end tag”.

sox.py
        #-- 3
        # [ if contentList is empty ->
        #     self.outFile  +:=  an empty tag with name (uTag)
        #         and attributes made from (attribs)
        #   else ->
        #     self.outFile  +:=  (an empty tag with name (uTag)
        #         and attributes made from (attribs)) +
        #         (concatenation of content) + (an end tag with
        #         name (uTag)) ]
        if len(contentList) == 0:
            self._emptyTag(uTag, attribs)
        else:
            self._startTag(uTag, attribs)
            self.write(*contentList)
            self._endTag(uTag)