1 #!/usr/bin/env python 2 3 """ 4 Utility functions for XSLForms documents. 5 6 Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk> 7 8 This library is free software; you can redistribute it and/or 9 modify it under the terms of the GNU Lesser General Public 10 License as published by the Free Software Foundation; either 11 version 2.1 of the License, or (at your option) any later version. 12 13 This library is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 Lesser General Public License for more details. 17 18 You should have received a copy of the GNU Lesser General Public 19 License along with this library; if not, write to the Free Software 20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 """ 22 23 def add_elements(positions, element_name, element_parent_name=None): 24 25 """ 26 At the specified 'positions' in a document, add a new element of the given 27 'element_name'. If the optional 'element_parent_name' is specified, ensure 28 the presence of special parent elements bearing that name, adding them at 29 the specified 'positions' where necessary, before adding the elements with 30 the stated 'element_name' beneath such parent elements. 31 """ 32 33 if not positions: 34 return 35 for position in positions: 36 if element_parent_name: 37 parent_elements = position.xpath(element_parent_name) 38 if not parent_elements: 39 parent_element = position.ownerDocument.createElementNS(None, element_parent_name) 40 position.appendChild(parent_element) 41 else: 42 parent_element = parent_elements[0] 43 else: 44 parent_element = position 45 parent_element.appendChild(position.ownerDocument.createElementNS(None, element_name)) 46 47 def remove_elements(positions): 48 49 """ 50 Remove the elements located at the given 'positions'. 51 """ 52 53 if not positions: 54 return 55 for position in positions: 56 position.parentNode.removeChild(position) 57 58 # vim: tabstop=4 expandtab shiftwidth=4