paulb@78 | 1 | #!/usr/bin/env python |
paulb@78 | 2 | |
paulb@78 | 3 | "Utility functions for XSLForms documents." |
paulb@78 | 4 | |
paulb@78 | 5 | def add_elements(positions, element_name, element_parent_name=None): |
paulb@78 | 6 | |
paulb@78 | 7 | """ |
paulb@78 | 8 | At the specified 'positions' in a document, add a new element of the given |
paulb@78 | 9 | 'element_name'. If the optional 'element_parent_name' is specified, ensure |
paulb@78 | 10 | the presence of special parent elements bearing that name, adding them at |
paulb@78 | 11 | the specified 'positions' where necessary, before adding the elements with |
paulb@78 | 12 | the stated 'element_name' beneath such parent elements. |
paulb@78 | 13 | """ |
paulb@78 | 14 | |
paulb@78 | 15 | if not positions: |
paulb@78 | 16 | return |
paulb@78 | 17 | for position in positions: |
paulb@78 | 18 | if element_parent_name: |
paulb@78 | 19 | parent_elements = position.xpath(element_parent_name) |
paulb@78 | 20 | if not parent_elements: |
paulb@78 | 21 | parent_element = position.ownerDocument.createElementNS(None, element_parent_name) |
paulb@78 | 22 | position.appendChild(parent_element) |
paulb@78 | 23 | else: |
paulb@78 | 24 | parent_element = parent_elements[0] |
paulb@78 | 25 | else: |
paulb@78 | 26 | parent_element = position |
paulb@78 | 27 | parent_element.appendChild(position.ownerDocument.createElementNS(None, element_name)) |
paulb@78 | 28 | |
paulb@78 | 29 | def remove_elements(positions): |
paulb@78 | 30 | |
paulb@78 | 31 | """ |
paulb@78 | 32 | Remove the elements located at the given 'positions'. |
paulb@78 | 33 | """ |
paulb@78 | 34 | |
paulb@78 | 35 | if not positions: |
paulb@78 | 36 | return |
paulb@78 | 37 | for position in positions: |
paulb@78 | 38 | position.parentNode.removeChild(position) |
paulb@78 | 39 | |
paulb@78 | 40 | # vim: tabstop=4 expandtab shiftwidth=4 |