2

The name of the tag I am trying to get to is {http://whitehatsec.com/XML-api-Vuln}description. Conveniently every tag is prefixed with that lovely reference to the whitehat website. Unfortunately the xpath in lxml doesn't like it. I am currently trying vuln_root[0].xpath('//\{http://whitehatsec.com/XML-api-Vuln\}description') which should get me to the right node. However lxml keeps saying

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

How can I get around this awful tag naming in my xpath? Thanks

thaweatherman
  • 1,467
  • 4
  • 20
  • 32
  • `{namespace-uri}element-name` is one way to write an element name including its namespace (sometimes you'll also see these with a leading `Q` before the opening curly brace). It's the same as `ns-prefix:element-name` when you have a `xmlns:ns-prefix='namespace-uri'` declaration. – Charles Duffy Jun 27 '14 at 22:01
  • For a bit more background: "It's a feature, not a bug!" -- XML namespaces let folks build compound documents without worrying about conflicts... so you could build a document that included both XHTML or XForms or whatever else and whitehat data, and not worry about whether any of the tag names conflict with each other. It's exceptionally useful combining things like XML template/macro languages with other document types, since the namespaces tell your editing tools for your larger documents that the markup content is foreign data and should be left alone. – Charles Duffy Jun 27 '14 at 22:17

1 Answers1

2

The easiest approach here is to just map the namespace.

el.xpath('//vuln:description',
    namespaces={'vuln': 'http://whitehatsec.com/XML-api-Vuln'})
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441