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 ishttp://www.inkscape.org/namespaces/inkscape, and depending on your XML,inkscape:groupmodemight be identical tofoo: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:Which would get you:
To add in the inkscape-specific attributes, you would do this:
Which would get you:
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:With this in place, the final output would look like this:
More information in the LXML documentation.