Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python lxml inkscape命名空间标记_Python_Svg_Lxml_Inkscape - Fatal编程技术网

python lxml inkscape命名空间标记

python lxml inkscape命名空间标记,python,svg,lxml,inkscape,Python,Svg,Lxml,Inkscape,我正在生成一个SVG文件,打算包含Inkscape特定的标记。例如,inkscape:label和inkscape:groupmode。我使用lxml-etree作为解析器/生成器。我想将标签和组模式标记添加到以下实例: layer = etree.SubElement(svg_instance, 'g', id="layer-id") 我的问题是如何实现这一点,以便在SVG中获得正确的输出形式,例如: <g inkscape:groupmode="layer" id="layer-id

我正在生成一个SVG文件,打算包含Inkscape特定的标记。例如,
inkscape:label
inkscape:groupmode
。我使用lxml-etree作为解析器/生成器。我想将
标签
组模式
标记添加到以下实例:

layer = etree.SubElement(svg_instance, 'g', id="layer-id")
我的问题是如何实现这一点,以便在SVG中获得正确的输出形式,例如:

<g inkscape:groupmode="layer" id="layer-id" inkscape:label="layer-label">

首先,请记住,
inkscape:
不是名称空间,它只是引用XML根元素中定义的名称空间的一种方便方法。名称空间是
http://www.inkscape.org/namespaces/inkscape
,并且取决于您的XML,
inkscape:groupmode
可能与
foo:groupmode
相同。当然,您的
元素是SVG名称空间的一部分,
http://www.w3.org/2000/svg
。要使用LXML生成适当的输出,可以从以下内容开始:

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')
<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>
这会让你:

<ns0:svg xmlns:ns0="http://www.w3.org/2000/svg">
  <ns0:g id="layer-id"/>
</ns0:svg>
<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>
这会让你:

<ns0:svg xmlns:ns0="http://www.w3.org/2000/svg">
  <ns0:g id="layer-id"/>
</ns0:svg>
<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>
有了它,最终输出将如下所示:

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')
<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>

更多信息请参阅