Project

General

Profile

« Previous | Next » 

Revision 1005

xpath.py: get(): Go to root when empty element is encountered at the beginning of an XPath. Added allow_rooted parameter to turn off this functionality when XPaths with a leading slash should not be considered rooted.

View differences:

lib/xpath.py
166 166

  
167 167
##### Querying/creating XML trees
168 168

  
169
def get(root, xpath, create=False, last_only=None, limit=1):
169
def get(root, xpath, create=False, last_only=None, limit=1, allow_rooted=True):
170 170
    '''Warning: The last_only optimization may put data that should be together
171 171
    into separate nodes'''
172 172
    if last_only == None: last_only = create
173 173
    if limit == None or limit > 1: last_only = False
174 174
    if util.is_str(xpath): xpath = parse(xpath)
175 175
    
176
    # Handle edge cases
176 177
    if xpath == []: return [root]
177 178
    if create and not is_positive(xpath): return []
179
    
180
    # Define vars
178 181
    doc = root.ownerDocument
182
    if allow_rooted and is_rooted(xpath): root = doc.documentElement
179 183
    elem = xpath[0]
180 184
    
181 185
    # Find possible matches
......
251 255
                else: last_only = False
252 256
                set_id(xpath, id_)
253 257
        else: root = node
254
        next += get(root, xpath, create, last_only, limit)
258
        next += get(root, xpath, create, last_only, limit, allow_rooted=False)
255 259
        
256 260
        for branch in elem.other_branches:
257 261
            branch = copy.deepcopy(branch)
258 262
            set_value(branch, value_)
259
            next += get(node, branch, create, last_only, limit)
263
            next += get(node, branch, create, last_only, limit,
264
                allow_rooted=False)
260 265
    
261 266
    return next
262 267

  
bin/map
167 167
            in_db.close()
168 168
        elif in_is_xml:
169 169
            def get_value(in_, row):
170
                nodes = xpath.get(row, in_)
170
                nodes = xpath.get(row, in_, allow_rooted=False)
171 171
                if nodes != []: return xml_dom.value(nodes[0])
172 172
                else: return None
173 173
            rows = xpath.get(doc0.documentElement, in_root, limit=end)

Also available in: Unified diff