I am generating an SVG file that's intended to include Inkscape-specific tags. For example, inkscape:label
and inkscape:groupmode
. I am using lxml etree as my parser/generator. I'd like to add the label
and groupmode
tags to the following instance:
layer = etree.SubElement(svg_instance, 'g', id="layer-id")
My question is how do I achieve that in order to get the correct output form in the SVG, for example:
<g inkscape:groupmode="layer" id="layer-id" inkscape:label="layer-label">
First, remember that inkscape:
isnt' a namespace, it's just a convenient way of referring to a namespace that is defined in your XML root element. The namespace is http://www.inkscape.org/namespaces/inkscape
, and depending on your XML, inkscape:groupmode
might be identical to foo:groupmode
. And of course, your <g>
element is part of the SVG namespace, http://www.w3.org/2000/svg
. To generate appropriate output with LXML, you would start with something like this:
from lxml import etree
root = etree.Element('{http://www.w3.org/2000/svg}svg')
g = etree.SubElement(root, '{http://www.w3.org/2000/svg}g', id='layer-id')
Which would get you:
<ns0:svg xmlns:ns0="http://www.w3.org/2000/svg">
<ns0:g id="layer-id"/>
</ns0:svg>
To add in the inkscape-specific attributes, you would do this:
g.set('{http://www.inkscape.org/namespaces/inkscape}groupmode', 'layer')
g.set('{http://www.inkscape.org/namespaces/inkscape}label', 'layer-label')
Which would get you:
<ns0:svg xmlns:ns0="http://www.w3.org/2000/svg">
<ns0:g xmlns:ns1="http://www.inkscape.org/namespaces/inkscape" id="layer-id" ns1:groupmode="layer" ns1:label="layer-label"/>
</ns0:svg>
Which believe it or not is exactly what you want. You can clean up the namespace labels a bit by passing an nsmap=
parameter when creating your root element. Like this:
NSMAP = {
None: 'http://www.w3.org/2000/svg',
'inkscape': 'http://www.inkscape.org/namespaces/inkscape',
}
root = etree.Element('{http://www.w3.org/2000/svg}svg', nsmap=NSMAP)
With this in place, the final output would look like this:
<svg xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns="http://www.w3.org/2000/svg">
<g id="layer-id" inkscape:label="layer-label" inkscape:groupmode="layer"/>
</svg>
More information in the LXML documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With