在 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/