Revision 1004
Added by Aaron Marcuse-Kubitza almost 13 years ago
lib/xpath.py | ||
---|---|---|
10 | 10 |
|
11 | 11 |
class XpathElem: |
12 | 12 |
def __init__(self, name='', value=None, is_attr=False): |
13 |
if name == '': name = '.' |
|
14 | 13 |
self.name = name |
15 | 14 |
self.value = value |
16 | 15 |
self.is_attr = is_attr |
... | ... | |
24 | 23 |
str_ = '' |
25 | 24 |
if not self.is_positive: str_ += '!' |
26 | 25 |
if self.is_attr: str_ += '@' |
27 |
str_ += self.name |
|
26 |
if self.name == '': str_ += '""' |
|
27 |
else: str_ += self.name |
|
28 | 28 |
if self.keys != []: str_ += repr(self.keys) |
29 | 29 |
if self.attrs != []: str_ += ':'+repr(self.attrs) |
30 | 30 |
if self.is_ptr: str_ += '->' |
... | ... | |
38 | 38 |
|
39 | 39 |
def elem_is_empty(elem): return elem == empty_elem |
40 | 40 |
|
41 |
def is_self(elem): return elem.name == '' or elem.name == '.' |
|
42 |
|
|
43 |
def is_parent(elem): return elem.name == '..' |
|
44 |
|
|
41 | 45 |
##### Paths |
42 | 46 |
|
43 | 47 |
def is_positive(path): return path[0].is_positive |
... | ... | |
62 | 66 |
def set_id(path, id_, has_types=True): |
63 | 67 |
if has_types: id_level = instance_level |
64 | 68 |
else: id_level = 0 # root's children |
65 |
if path[0].name == '.': id_level += 1 # explicit root element
|
|
69 |
if is_self(path[0]): id_level += 1 # explicit root element
|
|
66 | 70 |
path[id_level].keys.append([XpathElem('id', id_, True)]) |
67 | 71 |
|
68 | 72 |
def is_id(path): return path[0].is_attr and path[0].name == 'id' |
... | ... | |
179 | 183 |
if elem.is_attr: |
180 | 184 |
child = root.getAttributeNode(elem.name) |
181 | 185 |
if child != None: children = [child] |
182 |
elif elem.name == '.': children = [root]
|
|
183 |
elif elem.name == '..':
|
|
186 |
elif is_self(elem): children = [root]
|
|
187 |
elif is_parent(elem):
|
|
184 | 188 |
parent = xml_dom.parent(root) |
185 | 189 |
if parent != None: children = [parent] |
186 | 190 |
else: return [] # don't try to create the document root's parent |
Also available in: Unified diff
xpath.py: Don't consider a path starting with "." to be rooted. Do this by not automatically translating an empty path name to ".".