Project

General

Profile

« Previous | Next » 

Revision 2001

xpath.py: set_id(): Return the path to the ID attr, which can be used to change the ID

View differences:

xpath.py
81 81

  
82 82
def set_id(path, id_, has_types=True):
83 83
    '''Caller must make a shallow copy of the path to prevent modifications from
84
    propagating to other copies of the path (a deep copy is not needed)'''
84
    propagating to other copies of the path (a deep copy is not needed).
85
    @return The path to the ID attr, which can be used to change the ID
86
    '''
85 87
    if has_types: id_level = instance_level
86 88
    else: id_level = 0 # root's children
87 89
    if is_self(path[0]): id_level += 1 # explicit root element
......
89 91
    id_elem = path[id_level] = copy.copy(path[id_level])
90 92
        # don't modify other copies of the path
91 93
    id_elem.keys = id_elem.keys[:] # don't modify other copies of the elem
92
    id_elem.keys.append([XpathElem('id', id_, True)])
94
    id_attr = XpathElem('id', None, is_attr=True)
95
    
96
    # Save the path to the ID attr
97
    id_path = copy.copy(path[:id_level+1]) # the path up through the ID level
98
    id_path.append(copy.copy(id_attr))
99
    
100
    # Set the ID attr on the provided XPath
101
    id_attr.value = id_
102
    id_elem.keys.append([id_attr])
103
    
104
    return id_path
93 105

  
94 106
def is_id(path): return path[0].is_attr and path[0].name == 'id'
95 107

  

Also available in: Unified diff