Alex
Alex

Reputation: 1999

lxml xpath filter with OR condition not working

The langs object is a lxml object produced by parsing this file: http://www.loc.gov/standards/codelists/languages.xml

This xpath works:

langs.node.xpath("//lang:language[lang:name='English']", namespaces={'lang':'info:lc/xmlns/codelist-v1'})[0].findtext('lang:name', namespaces={'lang': 'info:lc/xmlns/codelist-v1'})

When I add an additional condition of | lang:code = 'English' like so:

langs.node.xpath("//lang:language[lang:name='English' | lang:code='English']", namespaces={'lang':'info:lc/xmlns/codelist-v1'})[0].findtext('lang:name', namespaces={'lang': 'info:lc/xmlns/codelist-v1'})

I get the error:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "lxml.etree.pyx", line 1509, in lxml.etree._Element.xpath (src/lxml /lxml.etree.c:50717)
  File "xpath.pxi", line 318, in lxml.etree.XPathElementEvaluator.__call__ (src/lxml/lxml.etree.c:145969)
  File "xpath.pxi", line 238, in lxml.etree._XPathEvaluatorBase._handle_result (src/lxml/lxml.etree.c:144977)
  File "xpath.pxi", line 223, in lxml.etree._XPathEvaluatorBase._raise_eval_error  (src/lxml/lxml.etree.c:144785)
XPathEvalError: Invalid type

Upvotes: 3

Views: 2969

Answers (2)

Alex
Alex

Reputation: 1999

10 more minutes of googling and I had the answer:

I had the change | to or

langs.node.xpath("//lang:language[lang:name='English' or lang:code='English']", namespaces={'lang':'info:lc/xmlns/codelist-v1'})[0].findtext('lang:name', namespaces={'lang': 'info:lc/xmlns/codelist-v1'})

Upvotes: 3

Charles Duffy
Charles Duffy

Reputation: 295403

The correct syntax is or, not |:

nsmap={'lang': 'info:lc/xmlns/codelist-v1'}
langs.node.xpath("//lang:language[lang:name='English' or lang:code='English']",
  namespaces=nsmap)[0].findtext('lang:name', namespaces=nsmap)

Upvotes: 7

Related Questions