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