python lxml inkscape namespace tags

て烟熏妆下的殇ゞ 提交于 2019-12-05 12:10:29

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!