A pseudocode python variant could look like

   with xml.element("port") as e:
       e.attribute("name", name)
       e.attribute("location", location)
And that's it
Pretty much how https://github.com/Knio/dominate does it, but no context variable binding (otherwise gets messy with lots of nesting)