Revision 1700
Added by Aaron Marcuse-Kubitza almost 13 years ago
bin/map | ||
---|---|---|
154 | 154 |
in_is_xml = in_is_xpaths and not in_is_db |
155 | 155 |
out_is_xml_ref[0] = out_is_xpaths and not out_is_db |
156 | 156 |
|
157 |
if in_is_xml: |
|
158 |
doc0 = minidom.parse(sys.stdin) |
|
159 |
doc0_root = doc0.documentElement |
|
160 |
if out_label == None: out_label = doc0_root.tagName |
|
161 |
|
|
162 | 157 |
def process_rows(process_row, rows): |
163 | 158 |
'''Processes input rows |
164 | 159 |
@param process_row(in_row, i) |
... | ... | |
211 | 206 |
|
212 | 207 |
return map_rows(get_value, util.WrapIter(wrap_row, rows)) |
213 | 208 |
|
214 |
if map_path == None: |
|
215 |
iter_ = xml_dom.NodeElemIter(doc0_root) |
|
216 |
util.skip(iter_, xml_dom.is_text) # skip metadata |
|
217 |
row_ct = process_rows(lambda row, i: root.appendChild(row), iter_) |
|
218 |
elif in_is_db: |
|
209 |
if in_is_db: |
|
219 | 210 |
assert in_is_xpaths |
220 | 211 |
|
221 | 212 |
in_db = connect_db(in_db_config) |
... | ... | |
226 | 217 |
|
227 | 218 |
in_db.close() |
228 | 219 |
elif in_is_xml: |
229 |
rows = xpath.get(doc0_root, in_root, limit=end) |
|
230 |
if rows == []: raise SystemExit('Map error: Root "'+in_root |
|
231 |
+'" not found in input') |
|
232 |
|
|
233 |
def get_value(in_, row): |
|
234 |
in_ = './{'+(','.join(strings.with_prefixes(['']+prefixes, |
|
235 |
in_)))+'}' # also with no prefix |
|
236 |
nodes = xpath.get(row, in_, allow_rooted=False) |
|
237 |
if nodes != []: return xml_dom.value(nodes[0]) |
|
238 |
else: return None |
|
239 |
|
|
240 |
row_ct = map_rows(get_value, rows) |
|
220 |
doc0_root = minidom.parse(sys.stdin).documentElement |
|
221 |
if map_path == None: |
|
222 |
iter_ = xml_dom.NodeElemIter(doc0_root) |
|
223 |
util.skip(iter_, xml_dom.is_text) # skip metadata |
|
224 |
row_ct = process_rows(lambda row, i: root.appendChild(row), |
|
225 |
iter_) |
|
226 |
else: |
|
227 |
rows = xpath.get(doc0_root, in_root, limit=end) |
|
228 |
if rows == []: raise SystemExit('Map error: Root "'+in_root |
|
229 |
+'" not found in input') |
|
230 |
|
|
231 |
def get_value(in_, row): |
|
232 |
in_ = './{'+(','.join(strings.with_prefixes(['']+prefixes, |
|
233 |
in_)))+'}' # also with no prefix |
|
234 |
nodes = xpath.get(row, in_, allow_rooted=False) |
|
235 |
if nodes != []: return xml_dom.value(nodes[0]) |
|
236 |
else: return None |
|
237 |
|
|
238 |
row_ct = map_rows(get_value, rows) |
|
241 | 239 |
else: # input is CSV |
242 | 240 |
map_ = dict(mappings) |
243 | 241 |
reader, col_names = csvs.reader_and_header(sys.stdin) |
Also available in: Unified diff
bin/map: Merged XML inputs with and without a map into the in_is_xml section