Revision 998
Added by Aaron Marcuse-Kubitza almost 13 years ago
lib/xpath.py | ||
---|---|---|
148 | 148 |
|
149 | 149 |
def is_instance(elem): return elem.keys != [] and is_id(elem.keys[0]) |
150 | 150 |
|
151 |
def get(parent, xpath, create=False, last_only=None, limit=1):
|
|
151 |
def get(root, xpath, create=False, last_only=None, limit=1):
|
|
152 | 152 |
'''Warning: The last_only optimization may put data that should be together |
153 | 153 |
into separate nodes''' |
154 | 154 |
if last_only == None: last_only = create |
155 | 155 |
if limit == None or limit > 1: last_only = False |
156 | 156 |
if util.is_str(xpath): xpath = parse(xpath) |
157 | 157 |
|
158 |
if xpath == []: return [parent]
|
|
158 |
if xpath == []: return [root]
|
|
159 | 159 |
if create and not is_positive(xpath): return [] |
160 |
doc = parent.ownerDocument |
|
161 |
root = doc.documentElement |
|
160 |
doc = root.ownerDocument |
|
162 | 161 |
elem = xpath[0] |
163 | 162 |
|
164 | 163 |
# Find possible matches |
165 | 164 |
children = [] |
166 | 165 |
if elem.is_attr: |
167 |
child = parent.getAttributeNode(elem.name)
|
|
166 |
child = root.getAttributeNode(elem.name)
|
|
168 | 167 |
if child != None: children = [child] |
169 |
elif elem.name == '.': children = [parent]
|
|
168 |
elif elem.name == '.': children = [root]
|
|
170 | 169 |
else: |
171 |
children = xml_dom.by_tag_name(parent, elem.name,
|
|
170 |
children = xml_dom.by_tag_name(root, elem.name,
|
|
172 | 171 |
last_only and (elem.keys == [] or is_instance(elem))) |
173 | 172 |
|
174 | 173 |
# Check each match |
... | ... | |
187 | 186 |
if nodes == []: |
188 | 187 |
if not create: return [] |
189 | 188 |
if elem.is_attr: |
190 |
parent.setAttribute(elem.name, '')
|
|
191 |
node = parent.getAttributeNode(elem.name)
|
|
192 |
else: node = parent.appendChild(doc.createElement(elem.name))
|
|
189 |
root.setAttribute(elem.name, '')
|
|
190 |
node = root.getAttributeNode(elem.name)
|
|
191 |
else: node = root.appendChild(doc.createElement(elem.name))
|
|
193 | 192 |
if elem.value != None: xml_dom.set_value(node, elem.value) |
194 | 193 |
nodes.append(node) |
195 | 194 |
|
... | ... | |
205 | 204 |
|
206 | 205 |
# Follow pointer |
207 | 206 |
if elem.is_ptr: |
207 |
root = doc.documentElement |
|
208 | 208 |
xpath = copy.deepcopy(xpath) |
209 | 209 |
id_path = backward_id(xpath[instance_level]) |
210 | 210 |
if id_path != None: # backward (child-to-parent) pointer with ID key |
Also available in: Unified diff
xpath.py: get(): Renamed parent to root to better reflect that it's the starting point for the search. Calling it parent will later be confusing when we want to get the parent node using "..".