Project

General

Profile

« Previous | Next » 

Revision 18

Changed xml2db and data2xml to help standardize mapping to different XML formats

View differences:

scripts/xml2db/xml_util.py
46 46
    return True
47 47

  
48 48
def value(node):
49
    if node.firstChild != None: return node.firstChild.nodeValue
49
    if node.firstChild != None: return node.firstChild.nodeValue.strip()
50 50
    else: return None
scripts/data2xml/XpathParser.py
63 63
    
64 64
    def _name(self): return self._match_re(r'[\w.]+', required=True)
65 65
    
66
    def _value(self): return self._match_re(r'[\w|]+', required=True)
66
    def _value(self): return self._match_re(r'[\w.|]+', required=True)
scripts/data2xml/data2xml
16 16
        dest = sys.argv.pop(0)
17 17
        mappings_path = sys.argv.pop(0)
18 18
    except IndexError: raise Exception('Usage: '+prog_name
19
        +' dest_mappings_column mappings_path <dataset')
19
        +' dest_mappings_column mappings_path <dataset >output')
20 20
    
21 21
    # Get mappings
22 22
    mappings = {}
23
    has_type_containers = False # whether outer elements are type containiners
23 24
    stream = open(mappings_path, 'rb')
24 25
    reader = csv.reader(stream, delimiter=',', quotechar='"')
25 26
    fieldnames = reader.next()
......
28 29
    for row in reader:
29 30
        name = row[0]
30 31
        path = row[dest_idx]
31
        if path != '':
32
        if name != '' and path != '':
32 33
            if path.startswith('?'): path = path[1:]
33 34
            path = path.replace('<name>', name)
34
            path = re.sub(r'(?<=/)\*(?=s/(\w+))', r'\1', path)
35
            path, repl_ct = re.subn(r'(?<=/)\*(?=s/(\w+))', r'\1', path)
36
            if repl_ct > 0: has_type_containers = True # *s used for type elem
35 37
            mappings[name] = XpathParser(path).parse()
36 38
    stream.close()
39
    if has_type_containers: id_level = 1
40
    else: id_level = 0  
37 41
    
38 42
    # Process dataset
39 43
    doc = getDOMImplementation().createDocument(None, dest, None)
......
42 46
    fieldnames = reader.next()
43 47
    row_idx = 0
44 48
    for row in reader:
45
        row_id = src+'_'+str(row_idx)
49
        row_id = str(row_idx)
46 50
        for idx, name in enumerate(fieldnames):
47 51
            value = row[idx]
48
            if value and name in mappings:
52
            if value != '' and name in mappings:
49 53
                path = deepcopy(mappings[name]) # don't modify main value!
50
                path[1].attrs.append([XpathElem('id', row_id, is_attr=True)])
54
                path[id_level].attrs.append([XpathElem('id', row_id,
55
                    is_attr=True)])
51 56
                path[-1].value = value
52 57
                by_path(doc, path, True)
53 58
        row_idx += 1

Also available in: Unified diff