Python lxml XPATH——查找特定节点的所有父节点的属性

标签 python xpath svg lxml

在 SVG 文件中,我使用 xpath 查找包含相对 SVG 路径的节点,例如,

<path d="m -0.3,-0.3 l 0.6,0 l 0,0.6 l -0.6,0 l 0,-0.6 z" transform="translate(-1 -0.5)"/>

这个节点的各个父节点一直到根节点都有一个

transform="translate(X Y)"

决定上面相对路径实际放置位置的属性。捕获这条路径/节点的父节点一直到根的正确/简洁/有效的方法是什么,特别是那些具有“转换”属性的?

最佳答案

What is the right/concise/efficient way to capture the parents of this path/node all the way to the root, particularly those that have the 'transform' attribute?

使用:

ancestor::*[@transform]

这会选择所有具有 transform 属性的祖先元素。

可以直接选择祖先的所有transform属性:

ancestor::*/@transform

如果您只对那些字符串值以字符串“translate(”(可能以空格开头)开头的 transform 属性感兴趣,请使用:

ancestor::*/@transform[starts-with(normalize-space(@transform),'translate(')]

关于Python lxml XPATH——查找特定节点的所有父节点的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14227382/

相关文章:

python - 带有 Ipywidgets 和交互式图形的 Pyqt5/Pyside 应用程序

python - Pandas - 在分类数据中填充 NaN

python - 加入并计算每个子类别中的值数量

c# - XPath 选择匹配的节点

html - 为什么设置 View 宽度和高度不足以强制 SVG 元素的完整浏览器窗口渲染?

javascript - SVG 对象跟随路径

javascript - 如何访问 SVG 文档中的 <path>

python - 从 pandas 列表列创建堆栈

c# - HTMLAgilityPack SelectNodes 选择所有 <img> 元素

xml - XPath用于选择名称并按子属性进行过滤