upgrade_map_xml.py does not work with None values #742

Closed
artemp opened this Issue Oct 11, 2011 · 1 comment

Comments

Projects
None yet
2 participants
Owner

artemp commented Oct 11, 2011

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:

<LineSymbolizer> <CssParameter name="stroke-dasharray"/> </LineSymbolizer>

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

After (correct) conversion:

<LineSymbolizer stroke-dasharray="" />

Trying to write "<LineSymbolizer stroke-dasharray=''None'' />" 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:
}}}

Owner

artemp commented Oct 11, 2011

[qtvali] Just forget - as this tool is able to use three different etree classes and I've tested with just one of them ("import xml.etree.ElementTree as etree"), it might be reproducible on all systems; instead of:

{{{
try:
import lxml.etree as etree
HAS_LXML = True
except ImportError:
try:
import elementtree.ElementTree as etree
except ImportError:
import xml.etree.ElementTree as etree
}}}

Simply this should be used for initial testing:

{{{
import xml.etree.ElementTree as etree
}}}

Otherwise, this bug reproduces on Windows and Linux.

@springmeyer springmeyer closed this Sep 6, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment