def fragment_nesting(cls, elem1, tag2, namespaces=None):
        """for elem1 containing elements with tag2, 
        fragment elem1 into elems that are adjacent to and nested within tag2"""
        elems2 = elem1.xpath("child::%s" % tag2, namespaces=namespaces)
        while len(elems2) > 0:
            elem2 = elems2[0]
            parent2 = elem2.getparent()
            index2 = parent2.index(elem2)

            # all of elem2 has a new tag1 element embedded inside of it
            child_elem1 = etree.Element(elem1.tag)
            for k in elem1.attrib:
                child_elem1.set(k, elem1.get(k))
            elem2.text, child_elem1.text = '', elem2.text
            for ch in elem2.getchildren():
                child_elem1.append(ch)
            elem2.insert(0, child_elem1)

            # new_elem1 for all following children of parent2
            new_elem1 = etree.Element(elem1.tag)
            for k in elem1.attrib:
                new_elem1.set(k, elem1.get(k))
            new_elem1.text, elem2.tail = elem2.tail, ''
            for ch in parent2.getchildren()[index2 + 1 :]:
                new_elem1.append(ch)

            # elem2 is placed after parent2
            parent = parent2.getparent()
            parent.insert(parent.index(parent2) + 1, elem2)
            last_child = elem2

            # new_elem1 is placed after elem2
            parent.insert(parent.index(elem2) + 1, new_elem1)
            new_elem1.tail, elem1.tail = elem1.tail, ''

            XML.remove_if_empty(elem1)
            XML.remove_if_empty(new_elem1)

            # repeat until all tag2 elements are unpacked from the new_elem1
            elem1 = new_elem1
            elems2 = elem1.xpath("child::%s" % tag2, namespaces=namespaces)
def fragment_nesting ( cls , elem1 , tag2 , namespaces = None ) : elems2 = elem1 . xpath ( "child::%s" % tag2 , namespaces = namespaces ) while len ( elems2 ) > 0 : elem2 = elems2 [ 0 ] parent2 = elem2 . getparent ( ) index2 = parent2 . index ( elem2 ) child_elem1 = etree . Element ( elem1 . tag ) for k in elem1 . attrib : child_elem1 . set ( k , elem1 . get ( k ) ) elem2 . text , child_elem1 . text = '' , elem2 . text for ch in elem2 . getchildren ( ) : child_elem1 . append ( ch ) elem2 . insert ( 0 , child_elem1 ) new_elem1 = etree . Element ( elem1 . tag ) for k in elem1 . attrib : new_elem1 . set ( k , elem1 . get ( k ) ) new_elem1 . text , elem2 . tail = elem2 . tail , '' for ch in parent2 . getchildren ( ) [ index2 + 1 : ] : new_elem1 . append ( ch ) parent = parent2 . getparent ( ) parent . insert ( parent . index ( parent2 ) + 1 , elem2 ) last_child = elem2 parent . insert ( parent . index ( elem2 ) + 1 , new_elem1 ) new_elem1 . tail , elem1 . tail = elem1 . tail , '' XML . remove_if_empty ( elem1 ) XML . remove_if_empty ( new_elem1 ) elem1 = new_elem1 elems2 = elem1 . xpath ( "child::%s" % tag2 , namespaces = namespaces )
for elem1 containing elements with tag2, 
        fragment elem1 into elems that are adjacent to and nested within tag2