(stack_children, current_session=None)
return clean_children
Given a list of stack children (commands and datums)
    replace any references in the datum to session variables that
    have already been added to the session.

    e.g.
    <datum id="case_id_a" value="instance('commcaresession')/session/data/case_id_new_a"/>
    <datum id="case_id_b" value="instance('commcaresession')/session/data/case_id_a"/>
                                                                          ^^^^^^^^^
    In the second datum replace ``case_id_a`` with ``case_id_new_a``.

    We have to do this because stack create blocks do not update the session after each datum
    is added so items put into the session in one step aren't available to later steps.
Given a list of stack children (commands and datums)
    replace any references in the datum to session variables that
    have already been added to the session.
Given a list of stack children ( commands and datums ) replace any references in the datum to session variables that have already been added to the session .
def _replace_session_references_in_stack(stack_children, current_session=None):
    """Given a list of stack children (commands and datums)
    replace any references in the datum to session variables that
    have already been added to the session.

    e.g.
    <datum id="case_id_a" value="instance('commcaresession')/session/data/case_id_new_a"/>
    <datum id="case_id_b" value="instance('commcaresession')/session/data/case_id_a"/>
                                                                          ^^^^^^^^^
    In the second datum replace ``case_id_a`` with ``case_id_new_a``.

    We have to do this because stack create blocks do not update the session after each datum
    is added so items put into the session in one step aren't available to later steps.
    """
    current_session_vars = [datum.id for datum in current_session] if current_session else []
    clean_children = []
    child_map = {}
    for child in stack_children:
        if isinstance(child, StackQuery):
            for data in child.data:
                session_vars = session_var_regex.findall(data.ref)
                for var in session_vars:
                    if var in child_map and var not in current_session_vars:
                        data.ref = data.ref.replace(session_var(var), child_map[var])
            clean_children.append(child)
        elif isinstance(child, StackDatum):
            session_vars = session_var_regex.findall(child.value)
            new_value = child.value
            for var in session_vars:
                if var in child_map and var not in current_session_vars:
                    new_value = new_value.replace(session_var(var), child_map[var])

            child_map[child.id] = new_value
            clean_children.append(StackDatum(id=child.id, value=new_value))
        else:
            clean_children.append(child)

    return clean_children
def _replace_session_references_in_stack ( stack_children , current_session = None ) : current_session_vars = [ datum . id for datum in current_session ] if current_session else [ ] clean_children = [ ] child_map = { } for child in stack_children : if isinstance ( child , StackQuery ) : for data in child . data : session_vars = session_var_regex . findall ( data . ref ) for var in session_vars : if var in child_map and var not in current_session_vars : data . ref = data . ref . replace ( session_var ( var ) , child_map [ var ] ) clean_children . append ( child ) elif isinstance ( child , StackDatum ) : session_vars = session_var_regex . findall ( child . value ) new_value = child . value for var in session_vars : if var in child_map and var not in current_session_vars : new_value = new_value . replace ( session_var ( var ) , child_map [ var ] ) child_map [ child . id ] = new_value clean_children . append ( StackDatum ( id = child . id , value = new_value ) ) else : clean_children . append ( child ) return clean_children