由于名称空间为空,Python XPath lxml无法读取SVG路径元素? [英] Python XPath lxml could not read SVG path element due to empty namespace?

查看:120
本文介绍了由于名称空间为空,Python XPath lxml无法读取SVG路径元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SVG(Xml)文件,我想从中选择一些元素.出于MCRE的考虑,我已将文件缩减为此

I have an SVG (Xml) file from which I want to select some elements. For the sake of a MCRE I have cut down the file to this

<svg >
    <!-- xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" -->
    <g>
       <path style="fill:#19518b;fill-opacity:1;fill-rule:nonzero;stroke:none" />
       <path style="fill:#a80c3d;fill-opacity:1;fill-rule:nonzero;stroke:none" />
       <path style="fill:#a98b6e;fill-opacity:1;fill-rule:nonzero;stroke:none" />
   </g>
</svg>

在注释中可以找到根元素的一些可选名称空间属性,以便可以将它们重新插入以复制实际方案(充分赋予SVG根元素).

Where some optional namespace attributes for the root element are located in a comment so they can be inserted back in to replicate the real scenario (where SVG root element is fulsomely attributed).

从以下Xml(SVG)中,我想选择用fill:#19518b;fill-opacity:1;fill-rule:nonzero;stroke:none设置样式的元素.有一场比赛.以下代码适用于给定的Xml.

From the following Xml (SVG) I want to select the elements styled with fill:#19518b;fill-opacity:1;fill-rule:nonzero;stroke:none. There is one of match. The following code works on the given Xml.

from lxml import etree
sFileName = 'C:/Users/Simon/Downloads/pdf_skunkworks/inflation-report-may-2018-page6 - Copy.svg'

tree = etree.Parse(sFileName)
svgNamespace = "xmlns:svg='http://www.w3.org/2000/svg'"
#xpath = r"//svg:path[@style='fill:#19518b;fill-opacity:1;fill-rule:nonzero;stroke:none']"
xpath = r"//path[@style='fill:#19518b;fill-opacity:1;fill-rule:nonzero;stroke:none']"
Print (XPath)
#bluePaths = tree.xpath(xpath,namespaces={   'svg': svgNamespace  })
bluePaths = tree.XPath(XPath)

print (bluePaths[0])

,但是它可以在给定的Xml上使用,因为它不具有在真实的SVG文件中找到的名称空间属性.一旦我重新插入了名称空间属性

but it works on the given Xml because it does not have the namespace attributes that one finds in a real SVG file. Once I reinsert the namespace attributes thus

<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"    >

然后是Python代码(如给定的失败).我知道我需要使用名称空间,并且您可以在Python中看到我的尝试被注释掉,但是它们不起作用.名称空间前缀之一是空字符串,无法传递到名称空间字典.

then the Python code (as given fails). I know I need to use namespaces and you can see my attempts commented out in the Python but they don't work. One of the namespace prefix is an empty string and could not be passed to the namespace dictionary.

无论如何,在早上,我将写一个能够克隆SVG文件并从根元素中删除属性的方法,因为我知道这种方法有效.同时,如果有人能找到解决此问题的真正方法,那么我将不胜感激(克隆文件似乎不是最佳选择).

Anyway, in the morning I will write could to clone the SVG file and remove the attributes from the root element because I know this approach works. In the meantime if someone can figure the real way to solve this then I'd be grateful (cloning files seems suboptimal).

P.S. SVG是通过从命令行运行Inkscape创建的,我给出了一页pdf文件,并要求进行简单的svg导出.

P.S. The SVG is created from running Inkscape from the command line, I give a single page pdf and ask for plain svg export.

推荐答案

这是因为名称空间URI只是http://www.w3.org/2000/svg.

It's because the namespace URI is just http://www.w3.org/2000/svg.

更改:

svgNamespace = "xmlns:svg='http://www.w3.org/2000/svg'"

收件人:

svgNamespace = "http://www.w3.org/2000/svg"

这篇关于由于名称空间为空,Python XPath lxml无法读取SVG路径元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆