1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/XSLForms/Utils.py Thu Jul 14 16:03:24 2005 +0000
1.3 @@ -0,0 +1,40 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"Utility functions for XSLForms documents."
1.7 +
1.8 +def add_elements(positions, element_name, element_parent_name=None):
1.9 +
1.10 + """
1.11 + At the specified 'positions' in a document, add a new element of the given
1.12 + 'element_name'. If the optional 'element_parent_name' is specified, ensure
1.13 + the presence of special parent elements bearing that name, adding them at
1.14 + the specified 'positions' where necessary, before adding the elements with
1.15 + the stated 'element_name' beneath such parent elements.
1.16 + """
1.17 +
1.18 + if not positions:
1.19 + return
1.20 + for position in positions:
1.21 + if element_parent_name:
1.22 + parent_elements = position.xpath(element_parent_name)
1.23 + if not parent_elements:
1.24 + parent_element = position.ownerDocument.createElementNS(None, element_parent_name)
1.25 + position.appendChild(parent_element)
1.26 + else:
1.27 + parent_element = parent_elements[0]
1.28 + else:
1.29 + parent_element = position
1.30 + parent_element.appendChild(position.ownerDocument.createElementNS(None, element_name))
1.31 +
1.32 +def remove_elements(positions):
1.33 +
1.34 + """
1.35 + Remove the elements located at the given 'positions'.
1.36 + """
1.37 +
1.38 + if not positions:
1.39 + return
1.40 + for position in positions:
1.41 + position.parentNode.removeChild(position)
1.42 +
1.43 +# vim: tabstop=4 expandtab shiftwidth=4