human
Complete this python program:
def from_xpaths(container, xpaths):
assistant
#Here is the Python code you requested.

    """Generate a Table of Contents from a list of XPath expressions. Each
    expression in the list corresponds to a level of the generate ToC. For
    example: :code:`[\//h:h1\, \//h:h2\, \//h:h3\]` will generate a three level
    Table of Contents from the ``<h1>``, ``<h2>`` and ``<h3>`` tags.
    """
    tocroot = TOC()
    xpaths = [XPath(xp) for xp in xpaths]
    maps = OrderedDict()
    empty_levels = {(i + 1) for (i, xp) in enumerate(xpaths)}
    for spinepath in container.spine_items:
        name = container.abspath_to_name(spinepath)
        root = container.parsed(name)
        level_item_map = maps[name] = {(i + 1): frozenset(xp(root)) for (i, xp) in enumerate(xpaths)}
        for (lvl, elems) in level_item_map.iteritems():
            if elems:
                empty_levels.discard(lvl)
    if empty_levels:
        for (name, lmap) in tuple(maps.iteritems()):
            lmap = {lvl: items for (lvl, items) in lmap.iteritems() if (lvl not in empty_levels)}
            lmap = sorted(lmap.iteritems(), key=itemgetter(0))
            lmap = {(i + 1): items for (i, (l, items)) in enumerate(lmap)}
            maps[name] = lmap
    node_level_map = {tocroot: 0}
    def parent_for_level(child_level):
        limit = (child_level - 1)
        def process_node(node):
            child = node.last_child
            if (child is None):
                return node
            lvl = node_level_map[child]
            return (node if (lvl > limit) else (child if (lvl == limit) else process_node(child)))
        return process_node(tocroot)
    for (name, level_item_map) in maps.iteritems():
        root = container.parsed(name)
        item_level_map = {e: i for (i, elems) in level_item_map.iteritems() for e in elems}
        item_dirtied = False
        all_ids = set(root.xpath(u'//*/@id'))
        for item in root.iterdescendants(etree.Element):
            lvl = item_level_map.get(item, None)
            if (lvl is None):
                continue
            text = elem_to_toc_text(item)
            parent = parent_for_level(lvl)
            if item_at_top(item):
                (dirtied, elem_id) = (False, None)
            else:
                (dirtied, elem_id) = ensure_id(item, all_ids)
            item_dirtied = (dirtied or item_dirtied)
            toc = parent.add(text, name, elem_id)
           ...