我试图用lxml
解析SVG文档。我的代码是:
nsmap = {
'svg': 'http://www.w3.org/2000/svg',
'xlink': 'http://www.w3.org/1999/xlink',
}
root = etree.XML(svg)
# this works (finds the element with the given ID)
root.xpath('./svg:g/svg:g/svg:g[@id="route_1_edge"]', namespaces=nsmap)
# this yields "XPathEvalError: Invalid expression"
root.xpath('./svg:g/svg:g/svg:g[fn:startswith(@id,"route_1")]', namespaces=nsmap)
有人知道为什么第一个有效,第二个不起作用吗?如果我将第三个svg:g
改为svg:text
我没有得到异常,因此它似乎与g
元素有关,特别是它不喜欢的元素,不过,简单的g[@id="foo"]
搜索也很好。在
“startswith”函数拼写为starts-with。另外,省略
fn:
。在^{pr2}$
收益率
相关问题 更多 >
编程相关推荐