mapnik-org/mapnik — Issue #740: upgrade_map_xml.py does not work with None values
Author: artemp
Created: 2011-10-11 08:10:04+00:00
State: open
Labels: Autotools

The following patch solves problem with XML conversion scripts from mapnik1 to mapnik2.

Problem description:
If there are tags with empty values, which should be converted to attributes with empty values, Python will use None as tag value, but it will give error when writing None as attribute value; thus, there must be recursive search for None's from whole XML tree after doing conversion and before writing it to file; all None's must be replaced with empty strings.

Problem example:

{{{

}}}

Here, stroke-dasharray is empty tag, therefore with value None (instead of "").

After (correct) conversion:

{{{

}}}

Trying to write "" into file will result in error, because only string type is allowed by XML standard and etree class accepts only that type.

Patch:

{{{
+++ b/upgrade_map_xml.py
@@ -202,7 +202,15 @@ def upgrade(input_xml,output_xml=None,indent_xml=True):

if indent_xml:
indent(root)
-
+
+ def none_to_empty_string_rec(tree):
+ for a in tree.findall('*'):
+ for k, v in a.attrib.items():
+ if v == None:
+ a.attrib[k] = ""
+ none_to_empty_string_rec(a)
+ none_to_empty_string_rec(tree)
+
if output_xml:
tree.write(output_xml)
else:
}}}