使用substring-after为每个元素返回一个字符串

时间:2017-02-28 10:36:24

标签: python xpath substring lxml

如果我映射到element属性,我可以返回一个字符串列表:

In [15]: root
Out[15]: <Element ADI at 0x7f4b5e6ad188>

In [16]: root = et.fromstring('<a><c><b value="the.never.ending.tale.99999.gz"/><b value="the.never.ending.tale.999745745799.gz"/><b value="the.never.ending.tale.99951351399.gz"/></c></a>')

In [23]: root.xpath('./c/b/@value')
Out[23]:
['the.never.ending.tale.99999.gz',
 'the.never.ending.tale.999745745799.gz',
 'the.never.ending.tale.99951351399.gz']

但是我无法使用以下命令返回子字符串列表:

In [24]: root.xpath('./c/b/substring-after(@value,"never")')
---------------------------------------------------------------------------
XPathEvalError                            Traceback (most recent call last)
<ipython-input-24-16664f97a745> in <module>()
----> 1 root.xpath('./c/b/substring-after(@value,"never")')

src/lxml/lxml.etree.pyx in lxml.etree._Element.xpath (src/lxml/lxml.etree.c:57924)()

src/lxml/xpath.pxi in lxml.etree.XPathElementEvaluator.__call__ (src/lxml/lxml.etree.c:167085)()

src/lxml/xpath.pxi in lxml.etree._XPathEvaluatorBase._handle_result (src/lxml/lxml.etree.c:166044)()

XPathEvalError: Invalid expression

我在这里犯了什么错误?

1 个答案:

答案 0 :(得分:2)

正如评论中所指出的,你不能在XPath 1.0中编写单个表达式来返回一个序列或字符串列表,但是,如果你使用列表推导,你可以使用以下方法:

el = ET.fromstring('<a><c><b value="the.never.ending.tale.99999.gz"/><b value="the.never.ending.tale.999745745799.gz"/><b value="the.never.ending.tale.99951351399.gz"/></c></a>')
values = [b.xpath('substring-after(@value, "never")') for b in el.xpath('c/b')]

然后给出

>>> values
['.ending.tale.99999.gz', '.ending.tale.999745745799.gz', '.ending.tale.99951351399.gz']