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@600 | 8 | This program is free software; you can redistribute it and/or modify it under |
paulb@600 | 9 | the terms of the GNU Lesser General Public License as published by the Free |
paulb@600 | 10 | Software Foundation; either version 3 of the License, or (at your option) any |
paulb@600 | 11 | later version. |
paulb@116 | 12 | |
paulb@600 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paulb@600 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paulb@600 | 15 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
paulb@600 | 16 | details. |
paulb@116 | 17 | |
paulb@600 | 18 | You should have received a copy of the GNU Lesser General Public License along |
paulb@600 | 19 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paulb@116 | 20 | """ |
paulb@78 | 21 | |
paulb@520 | 22 | def add_elements(positions, *element_names): |
paulb@78 | 23 | |
paulb@78 | 24 | """ |
paulb@520 | 25 | At the specified 'positions' in a document, add a new element using the |
paulb@520 | 26 | first of the given 'element_names' inside any subsequent element named in |
paulb@520 | 27 | the 'element_names' list, in turn creating new parent elements for each |
paulb@520 | 28 | element name found. |
paulb@78 | 29 | """ |
paulb@78 | 30 | |
paulb@78 | 31 | if not positions: |
paulb@78 | 32 | return |
paulb@520 | 33 | reversed_element_names = list(element_names)[:] |
paulb@520 | 34 | reversed_element_names.reverse() |
paulb@78 | 35 | for position in positions: |
paulb@520 | 36 | element = position |
paulb@520 | 37 | for element_name in reversed_element_names[:-1]: |
paulb@520 | 38 | found_elements = element.xpath(element_name) |
paulb@520 | 39 | if not found_elements: |
paulb@520 | 40 | new_element = element.ownerDocument.createElementNS(None, element_name) |
paulb@520 | 41 | element.appendChild(new_element) |
paulb@78 | 42 | else: |
paulb@520 | 43 | new_element = found_elements[0] |
paulb@520 | 44 | element = new_element |
paulb@520 | 45 | new_element = element.ownerDocument.createElementNS(None, reversed_element_names[-1]) |
paulb@520 | 46 | element.appendChild(new_element) |
paulb@78 | 47 | |
paulb@78 | 48 | def remove_elements(positions): |
paulb@78 | 49 | |
paulb@78 | 50 | """ |
paulb@78 | 51 | Remove the elements located at the given 'positions'. |
paulb@78 | 52 | """ |
paulb@78 | 53 | |
paulb@78 | 54 | if not positions: |
paulb@78 | 55 | return |
paulb@78 | 56 | for position in positions: |
paulb@78 | 57 | position.parentNode.removeChild(position) |
paulb@78 | 58 | |
paulb@78 | 59 | # vim: tabstop=4 expandtab shiftwidth=4 |