1 #!/usr/bin/env python 2 3 "Utility functions for XSLForms documents." 4 5 def add_elements(positions, element_name, element_parent_name=None): 6 7 """ 8 At the specified 'positions' in a document, add a new element of the given 9 'element_name'. If the optional 'element_parent_name' is specified, ensure 10 the presence of special parent elements bearing that name, adding them at 11 the specified 'positions' where necessary, before adding the elements with 12 the stated 'element_name' beneath such parent elements. 13 """ 14 15 if not positions: 16 return 17 for position in positions: 18 if element_parent_name: 19 parent_elements = position.xpath(element_parent_name) 20 if not parent_elements: 21 parent_element = position.ownerDocument.createElementNS(None, element_parent_name) 22 position.appendChild(parent_element) 23 else: 24 parent_element = parent_elements[0] 25 else: 26 parent_element = position 27 parent_element.appendChild(position.ownerDocument.createElementNS(None, element_name)) 28 29 def remove_elements(positions): 30 31 """ 32 Remove the elements located at the given 'positions'. 33 """ 34 35 if not positions: 36 return 37 for position in positions: 38 position.parentNode.removeChild(position) 39 40 # vim: tabstop=4 expandtab shiftwidth=4