开发者

XPath: match multiple elements in one expression

I'm trying to do this (using lxml):

//*[@id="开发者_JAVA技巧32808345" or @id="33771423" or @id="15929470" or @id="33771117" or @id="15929266"]

in order to get all elements, no matter what tag, with the specified id's. I'm getting the following traceback:

invalid attribute predicate

this is how I'm generating the str (if that is relevant to the problem):

refs = ' or '.join('@id="%s"' % ref for ref in refs[0:5])
elements = etree.iterfind('//*[%s]' % refs)

EDIT, with the below solution I'm getting this error:

File "lxml.etree.pyx", line 1201, in lxml.etree._Element.iterchildren (src/lxml/lxml.etree.c:36294)
  File "lxml.etree.pyx", line 2163, in lxml.etree.ElementChildIterator.__init__ (src/lxml/lxml.etree.c:45331)
  File "lxml.etree.pyx", line 2118, in lxml.etree._ElementTagMatcher._initTagMatch (src/lxml/lxml.etree.c:44913)
  File "apihelpers.pxi", line 1413, in lxml.etree._getNsTag (src/lxml/lxml.etree.c:21412)
ValueError: Empty tag name


The normal way to do this is using the single pipe operator, I believe?

//*[@id="20"] | //*[@id="30"] | ... etc.

Do you have a snippet of the XML you're trying to do this on?


Try using the xpath method instead of iterfind.

I think the iterfind method doesn't accept all XPath expressions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜