XPathEvalError: Unregistered function for matches() in lxml
XPathEvalError: Unregistered function for matches() in lxml
我正在尝试在 python
中使用以下 xpath 查询
from lxml.html.soupparser import fromstring
root = fromstring(inString)
nodes = root.xpath(".//p3[matches(.,'ABC')]//preceding::p2//p3")
但它给了我错误
nodes = root.xpath(".//p3[matches(.,'ABC')]//preceding::p2//p3")
File "lxml.etree.pyx", line 1507, in lxml.etree._Element.xpath (src\lxml\lxml.etree.c:52198)
File "xpath.pxi", line 307, in lxml.etree.XPathElementEvaluator.__call__ (src\lxml\lxml.etree.c:152124)
File "xpath.pxi", line 227, in lxml.etree._XPathEvaluatorBase._handle_result (src\lxml\lxml.etree.c:151097)
File "xpath.pxi", line 212, in lxml.etree._XPathEvaluatorBase._raise_eval_error (src\lxml\lxml.etree.c:150896)
lxml.etree.XPathEvalError: Unregistered function
我如何在这里使用 XPath 2.0 函数和 lxml?
澄清
我之前使用的是 contains 函数,因为
nodes = root.xpath(".//p3[contains(text(),'ABC')]//preceding::p2//p3")
问题是我的 xml 文本中有换行符和空格,因此我尝试使用
nodes = root.xpath(".//p3[contains(normalize-space(),'ABC')]//preceding::p2//p3")
但这没有效果。最后我尝试使用 matches 函数,但我得到了错误。
样本XML
<doc>
<q></q>
<p1>
<p2 dd="ert" ji="pp">
<p3>1</p3>
<p3>2</p3>
<p3>
ABC
</p3>
<p3>3</p3>
</p2>
<p2 dd="ert" ji="pp">
<p3>4</p3>
<p3>5</p3>
<p3>ABC</p3>
<p3>6</p3>
</p2>
</p1>
<r></r>
<p1>
<p2 dd="ert" ji="pp">
<p3>7</p3>
<p3>8</p3>
<p3>ABC
</p3>
<p3>9</p3>
</p2>
<p2 dd="ert" ji="pp">
<p3>10</p3>
<p3>11</p3>
<p3>ABC</p3>
<p3>12</p3>
</p2>
</p1>
</doc>
how can i use XPath 2.0 functions here with lxml?
你不能(reference):
lxml supports XPath 1.0, XSLT 1.0 and the EXSLT extensions through libxml2 and libxslt in a standards compliant way.
在这种情况下,contains()
可能是最接近的:
.//p3[contains(., 'ABC')]//preceding::p2//p3
正如另一个答案中提到的,强调引用文档的另一部分,您可以使用 EXSLT 扩展来使用 lxml 实现正则表达式 match()
函数,例如:
......
ns = {"re": "http://exslt.org/regular-expressions"}
nodes = root.xpath(".//p3[re:match(.,'ABC')]//preceding::p2//p3", namespaces=ns)
我正在尝试在 python
中使用以下 xpath 查询from lxml.html.soupparser import fromstring
root = fromstring(inString)
nodes = root.xpath(".//p3[matches(.,'ABC')]//preceding::p2//p3")
但它给了我错误
nodes = root.xpath(".//p3[matches(.,'ABC')]//preceding::p2//p3")
File "lxml.etree.pyx", line 1507, in lxml.etree._Element.xpath (src\lxml\lxml.etree.c:52198)
File "xpath.pxi", line 307, in lxml.etree.XPathElementEvaluator.__call__ (src\lxml\lxml.etree.c:152124)
File "xpath.pxi", line 227, in lxml.etree._XPathEvaluatorBase._handle_result (src\lxml\lxml.etree.c:151097)
File "xpath.pxi", line 212, in lxml.etree._XPathEvaluatorBase._raise_eval_error (src\lxml\lxml.etree.c:150896)
lxml.etree.XPathEvalError: Unregistered function
我如何在这里使用 XPath 2.0 函数和 lxml?
澄清
我之前使用的是 contains 函数,因为
nodes = root.xpath(".//p3[contains(text(),'ABC')]//preceding::p2//p3")
问题是我的 xml 文本中有换行符和空格,因此我尝试使用
nodes = root.xpath(".//p3[contains(normalize-space(),'ABC')]//preceding::p2//p3")
但这没有效果。最后我尝试使用 matches 函数,但我得到了错误。
样本XML
<doc>
<q></q>
<p1>
<p2 dd="ert" ji="pp">
<p3>1</p3>
<p3>2</p3>
<p3>
ABC
</p3>
<p3>3</p3>
</p2>
<p2 dd="ert" ji="pp">
<p3>4</p3>
<p3>5</p3>
<p3>ABC</p3>
<p3>6</p3>
</p2>
</p1>
<r></r>
<p1>
<p2 dd="ert" ji="pp">
<p3>7</p3>
<p3>8</p3>
<p3>ABC
</p3>
<p3>9</p3>
</p2>
<p2 dd="ert" ji="pp">
<p3>10</p3>
<p3>11</p3>
<p3>ABC</p3>
<p3>12</p3>
</p2>
</p1>
</doc>
how can i use XPath 2.0 functions here with lxml?
你不能(reference):
在这种情况下,lxml supports XPath 1.0, XSLT 1.0 and the EXSLT extensions through libxml2 and libxslt in a standards compliant way.
contains()
可能是最接近的:
.//p3[contains(., 'ABC')]//preceding::p2//p3
正如另一个答案中提到的,强调引用文档的另一部分,您可以使用 EXSLT 扩展来使用 lxml 实现正则表达式 match()
函数,例如:
......
ns = {"re": "http://exslt.org/regular-expressions"}
nodes = root.xpath(".//p3[re:match(.,'ABC')]//preceding::p2//p3", namespaces=ns)