# HG changeset patch # User paulb # Date 1121357004 0 # Node ID 2dc3db5f3ce7785d861030c60ad8793734ec5827 # Parent 1c77a8db426740e1e7052c23bd5de1910062e34d [project @ 2005-07-14 16:03:24 by paulb] Added element addition/removal convenience functions for XML documents. diff -r 1c77a8db4267 -r 2dc3db5f3ce7 XSLForms/Utils.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/XSLForms/Utils.py Thu Jul 14 16:03:24 2005 +0000 @@ -0,0 +1,40 @@ +#!/usr/bin/env python + +"Utility functions for XSLForms documents." + +def add_elements(positions, element_name, element_parent_name=None): + + """ + At the specified 'positions' in a document, add a new element of the given + 'element_name'. If the optional 'element_parent_name' is specified, ensure + the presence of special parent elements bearing that name, adding them at + the specified 'positions' where necessary, before adding the elements with + the stated 'element_name' beneath such parent elements. + """ + + if not positions: + return + for position in positions: + if element_parent_name: + parent_elements = position.xpath(element_parent_name) + if not parent_elements: + parent_element = position.ownerDocument.createElementNS(None, element_parent_name) + position.appendChild(parent_element) + else: + parent_element = parent_elements[0] + else: + parent_element = position + parent_element.appendChild(position.ownerDocument.createElementNS(None, element_name)) + +def remove_elements(positions): + + """ + Remove the elements located at the given 'positions'. + """ + + if not positions: + return + for position in positions: + position.parentNode.removeChild(position) + +# vim: tabstop=4 expandtab shiftwidth=4