Gimp-Forum.net
Here again--re: xml parsing for svg - Printable Version

+- Gimp-Forum.net (https://www.gimp-forum.net)
+-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP)
+--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP)
+---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions)
+---- Thread: Here again--re: xml parsing for svg (/Thread-Here-again-re-xml-parsing-for-svg)



Here again--re: xml parsing for svg - carmen - 10-25-2019

Well, I began working on a wrapper for the default gimp-vectors-export-to-file, but
-- The option 'export all paths' doesn't work from a plugin, likely for the same reason as the bug in my previous tread
-- Anyway, I wish to export just my 'selected paths', to a single file, and not merged, but as individual paths in a single SVG file.

Well, as can be seen from the attachment, I got 'almost' there by using xml.etree.ElementTree--the trouble is I cannot get it to save to svg without mangling the file by adding a nsO: namespace to every tag.

Inkscape and Chrome can cope with the mangled file, but Gimp CANNOT--and one cannot find fault with that!

Is there some alternative to reprocessing the whole file (maybe as a string?) and removing all those spurious nsO:?

Thanks!


RE: Here again--re: xml parsing for svg - Ofnuts - 10-25-2019

OK, lemme spend the weekend on that, XML namespaces are always a pleasure (*)

Meanwhile:

Code:
imageExt=os.path.splitext(image.filename)[1]
imageName=os.path.splitext(image.filename)[0]

is best written using direct "unpacking":

Code:
imageName,imageExt=os.path.splitext(image.filename)

Also, to avoid hard-coding options number, I found a sleek technique. The whole thing is explained here.

(*) Not the whole weekend, mind Smile


RE: Here again--re: xml parsing for svg - Ofnuts - 10-25-2019

The "export all paths" option works, but you have to use None instead of 0.

Getting late here, more tomorrow.


RE: Here again--re: xml parsing for svg - Ofnuts - 10-26-2019

The solution is quite simple actually, you have to tell ElementTree that the default name space is the SVG name space:

Code:
ET.register_namespace('',"http://www.w3.org/2000/svg")
tree=ET.ElementTree(ET.fromstring(xml))

A bit of background: in complicated cases of XML (and people who like XML like complicated cases), you can have XML tags from several origins that unfortunately use the same name for different things. For instance, a geotracker application could have it own concept of "path" which is displayed using an SVG path. So to know which path is which, XML defines "namespaces", and here there would be one for the app, and one for SVG.

A namespace is defined by a unique string, which is usually a URLs. This is why you see the xmlns="http://www.w3.org/2000/svg" in the header. This says that the default name space is "http://www.w3.org/2000/svg". If there were several namespaces, the other ones would be defined by something like xmlns:geoapp="http://www.geoapp.com/geoappns".

Then in the XML file, to use the tags:

Code:
<!-- this is a tag in the default namespace -->
<path />
<!-- a tag with expliciit name space -->
<geoapp:path />

When you read the XML without a default namespace, Python add the namespace to all the tag names (if you look at the Python variables, the tags are named {http://www.w3.org/2000/svg}path}. So when you print, this information is not lost, because Python creates a namespace name on the fly and adds it to the tag, this is where your ns0 comes from. And everything will work if you amend the XML to declare ns0 as being the SVG namespace with an xmlns:ns0="http://www.w3.org/2000/svg". But you can also declare the namespace names with register_namespace() to let Python use the right names.


RE: Here again--re: xml parsing for svg - carmen - 10-27-2019

Quote: ...you have to tell ElementTree that the default name space is the SVG name space...
That was what I couldn't figure out how to do!
I dare say that the information is in either
http://effbot.org/zone/pythondoc-elementtree-ElementTree.htm or
https://docs.python.org/2.7/library/xml.etree.elementtree.html#parsing-xml-with-namespaces, or both,
but if so, I couldn't understand it.
Your explanation solved my problem... it's got to be a bad habit  Wink

Thanks also for the pointer to using namedtuple--by the way, there is a trifling typo in <name>Options=createOpts, which should be <name>Options=createOptions: it surfaces if one does copy->paste without paying much attention (admidtedly, a vice...)

As an aside: I have collected all the html documentation of your Ofn-Paths (the ones I have downloaded and use) into an epub (as a replacement for old chm). As you are the author, I would upload you a copy, but... 2Mb!--lots of images! So, I upload instead a copy of the cover I have made for it.
At least, I hope you can take it as a joke...

[attachment=3321]


RE: Here again--re: xml parsing for svg - Ofnuts - 10-27-2019

(10-27-2019, 08:49 AM)carmen Wrote:
Quote: ...you have to tell ElementTree that the default name space is the SVG name space...
That was what I couldn't figure out how to do!
I dare say that the information is in either
http://effbot.org/zone/pythondoc-elementtree-ElementTree.htm or
https://docs.python.org/2.7/library/xml.etree.elementtree.html#parsing-xml-with-namespaces, or both,
but if so, I couldn't understand it.
Your explanation solved my problem... it's got to be a bad habit  Wink

Always glad to help. Two good places to get general Python help: the friendly python-forum.io (where I used to be quite active), and the more formal StackOverflow.

(10-27-2019, 08:49 AM)carmen Wrote: Thanks also for the pointer to using namedtuple--by the way, there is a trifling typo in <name>Options=createOpts, which should be <name>Options=createOptions: it surfaces if one does copy->paste without paying much attention (admidtedly, a vice...)

Thanks, fixed.

(10-27-2019, 08:49 AM)carmen Wrote: As an aside: I have collected all the html documentation of your Ofn-Paths (the ones I have downloaded and use) into an epub (as a replacement for old chm). As you are the author, I would upload you a copy, but... 2Mb!--lots of images! So, I upload instead a copy of the cover I have made for it.
At least, I hope you can take it as a joke...

Nice cover! But I hope you scripted the generation, because things change quite often.


RE: Here again--re: xml parsing for svg - carmen - 10-27-2019

No trouble: I have only to re-open the guide in sigil and import the new version html--everithing gets updated, even images... maybe re-do toc, a mouse click.
Same for adding new html... I simply love open-code, even if the code itself is way over my head.