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@412 | 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@116 | 21 | """ |
paulb@78 | 22 | |
paulb@520 | 23 | def add_elements(positions, *element_names): |
paulb@78 | 24 | |
paulb@78 | 25 | """ |
paulb@520 | 26 | At the specified 'positions' in a document, add a new element using the |
paulb@520 | 27 | first of the given 'element_names' inside any subsequent element named in |
paulb@520 | 28 | the 'element_names' list, in turn creating new parent elements for each |
paulb@520 | 29 | element name found. |
paulb@78 | 30 | """ |
paulb@78 | 31 | |
paulb@78 | 32 | if not positions: |
paulb@78 | 33 | return |
paulb@520 | 34 | reversed_element_names = list(element_names)[:] |
paulb@520 | 35 | reversed_element_names.reverse() |
paulb@78 | 36 | for position in positions: |
paulb@520 | 37 | element = position |
paulb@520 | 38 | for element_name in reversed_element_names[:-1]: |
paulb@520 | 39 | found_elements = element.xpath(element_name) |
paulb@520 | 40 | if not found_elements: |
paulb@520 | 41 | new_element = element.ownerDocument.createElementNS(None, element_name) |
paulb@520 | 42 | element.appendChild(new_element) |
paulb@78 | 43 | else: |
paulb@520 | 44 | new_element = found_elements[0] |
paulb@520 | 45 | element = new_element |
paulb@520 | 46 | new_element = element.ownerDocument.createElementNS(None, reversed_element_names[-1]) |
paulb@520 | 47 | element.appendChild(new_element) |
paulb@78 | 48 | |
paulb@78 | 49 | def remove_elements(positions): |
paulb@78 | 50 | |
paulb@78 | 51 | """ |
paulb@78 | 52 | Remove the elements located at the given 'positions'. |
paulb@78 | 53 | """ |
paulb@78 | 54 | |
paulb@78 | 55 | if not positions: |
paulb@78 | 56 | return |
paulb@78 | 57 | for position in positions: |
paulb@78 | 58 | position.parentNode.removeChild(position) |
paulb@78 | 59 | |
paulb@78 | 60 | # vim: tabstop=4 expandtab shiftwidth=4 |