标记名称具有花括号时的XPath无效表达式

时间:2014-06-27 21:48:04

标签: python xml xpath lxml

我想要的标记名称为{http://whitehatsec.com/XML-api-Vuln}description。方便地,每个标签都以whitehat网站的可爱参考为前缀。不幸的是,xpath中的lxml并不喜欢它。我目前正在尝试vuln_root[0].xpath('//\{http://whitehatsec.com/XML-api-Vuln\}description')这应该让我到正确的节点。但是lxml一直在说

File "test.py", line 23, in <module> for s in vuln_root[0].xpath('//\{http://whitehatsec.com/XML-api-Vuln\}description'): File "lxml.etree.pyx", line 1509, in lxml.etree._Element.xpath (src/lxml/lxml.etree.c:50725) File "xpath.pxi", line 318, in lxml.etree.XPathElementEvaluator.__call__ (src/lxml/lxml.etree.c:146020) File "xpath.pxi", line 238, in lxml.etree._XPathEvaluatorBase._handle_result (src/lxml/lxml.etree.c:145028) File "xpath.pxi", line 224, in lxml.etree._XPathEvaluatorBase._raise_eval_error (src/lxml/lxml.etree.c:144883) lxml.etree.XPathEvalError: Invalid expression

如何绕过这个在我的xpath中命名的糟糕标签?感谢

1 个答案:

答案 0 :(得分:2)

这里最简单的方法是只映射名称空间。

el.xpath('//vuln:description',
    namespaces={'vuln': 'http://whitehatsec.com/XML-api-Vuln'})