Project

General

Profile

« Previous | Next » 

Revision 1009

xpath.py: Added get() support for references (different from pointers) to dynamically set the value of an attribute

View differences:

lib/xpath.py
1 1
# XPath parsing
2 2

  
3 3
import copy
4
import warnings
4 5

  
5 6
from Parser import Parser
6 7
import util
......
130 131
        
131 132
        # Value
132 133
        if parser.str_('='):
133
            if parser.str_('$'): value = _path()
134
            else: value = _value()
134
            if parser.str_('$'): # reference (different from a pointer)
135
                value = _path()
136
            else: value = _value() # literal value
135 137
            set_value(tree, value)
136 138
        
137 139
        # Expand * abbrs
......
196 198
        children = xml_dom.by_tag_name(root, elem.name,
197 199
        last_only and (elem.keys == [] or is_instance(elem)))
198 200
    
201
    # Retrieve elem value
202
    value_ = elem.value
203
    if util.is_list(value_): # reference (different from a pointer)
204
        targets = get(root, value_)
205
        try: target = targets[0]
206
        except IndexError:
207
            warnings.warn(UserWarning('XPath reference target missing: '
208
                +str(value_)+'\nXPath: '+str(xpath)))
209
            value_ = None
210
        else: value_ = xml_dom.value(target)
211
    
199 212
    # Check each match
200 213
    nodes = []
201 214
    for child in children:
202
        is_match = elem.value == None or xml_dom.value(child) == elem.value
215
        is_match = value_ == None or xml_dom.value(child) == value_
203 216
        for attr in elem.keys:
204 217
            if not is_match: break
205 218
            is_match = ((get(child, attr, False, last_only) != [])
......
215 228
            root.setAttribute(elem.name, '')
216 229
            node = root.getAttributeNode(elem.name)
217 230
        else: node = root.appendChild(doc.createElement(elem.name))
218
        if elem.value != None: xml_dom.set_value(node, elem.value)
231
        if value_ != None: xml_dom.set_value(node, value_)
219 232
        nodes.append(node)
220 233
    
221
    value_ = value(xpath)
234
    path_value = value(xpath)
222 235
    xpath = xpath[1:] # rest of XPath
223 236
    
224 237
    next = []
......
259 272
        
260 273
        for branch in elem.other_branches:
261 274
            branch = copy.deepcopy(branch)
262
            set_value(branch, value_)
275
            set_value(branch, path_value)
263 276
            next += get(node, branch, create, last_only, limit,
264 277
                allow_rooted=False)
265 278
    

Also available in: Unified diff