ho修复xpath的lxml
有人可以向我解释为什么Xptah在Elementree上的功能在元素上失败时起作用。
它正在工作
导入lxml.etree作为et
xml1 = et.parse(data1)
xml1.xpath(“ ws:book [ws:frome_information/ws:book_on_av ='1'或“
) “ WS:第2章/WS:has_international_assignment ='false']/ws:第3章/ws:book_type /text()“,namespace = namespace1)
- 输出 - ['常规','常规']
- 它不起作用
xp =” [ws:afre_information/ws:book_on_av ='1'或 ws:第3章/ws:book_type/text()
for book in xml1.findall("ws:Book", namespaces=NAMESPACE1):
print(book.xpath(xp, namespaces=NAMESPACE1))
WS:第2章/WS:has_international_assignment =' false
print(book.xpath(xp, namespaces=NAMESPACE1))
File "src\lxml\etree.pyx", line 1597, in lxml.etree._Element.xpath
File "src\lxml\xpath.pxi", line 305, in lxml.etree.XPathElementEvaluator.__call__
File "src\lxml\xpath.pxi", line 225, in lxml.etree._XPathEvaluatorBase._handle_result
lxml.etree.XPathEvalError: Invalid expression
'] /
<?xml version="1.0" encoding="UTF-8"?>
<ws:Books xmlns:ws="urn:com.aa/bb" >
<ws:Book>enter code here
<ws:Chapter1>
<ws:Data>
<ws:Type>Preferred</ws:Type>
<ws:Book_Name>Book1</ws:Book_Name>
</ws:Data>
</ws:Chapter1>
<ws:Chapter2>
<ws:Has_International_Assignment>true</ws:Has_International_Assignment>
</ws:Chapter2>
<ws:Chapter3>
<ws:Operation>ADD</ws:Operation>
<ws:Book_Type ws:PriorValue="">Regular</ws:Book_Type>
</ws:Chapter3>
<ws:Additional_Information>
<ws:Book_on_AV>1</ws:Book_on_AV>
</ws:Additional_Information>
</ws:Book>
<ws:Book>
<ws:Chapter1>
<ws:Data>
<ws:Type>Preferred</ws:Type>
<ws:Book_Name>Book2</ws:Book_Name>
</ws:Data>
</ws:Chapter1>
<ws:Chapter2>
<ws:Has_International_Assignment>true</ws:Has_International_Assignment>
</ws:Chapter2>
<ws:Chapter3>
<ws:Operation>ADD</ws:Operation>
<ws:Book_Type ws:PriorValue="">Regular</ws:Book_Type>
</ws:Chapter3>
<ws:Additional_Information>
<ws:Book_on_AV>1</ws:Book_on_AV>
</ws:Additional_Information>
</ws:Book>
</ws:Books>
Can somebody explain me why xptah on elementree works while it fails on element.
It's working
import lxml.etree as et
xml1 = et.parse(DATA1)
xml1.xpath("ws:Book[ws:Additional_Information/ws:Book_on_AV='1' or "
"ws:Chapter2/ws:Has_International_Assignment='false']/ws:Chapter3/ws:Book_Type
/text()", namespaces=NAMESPACE1)
--Output--
['Regular', 'Regular']
- It's not working
xp = "[ws:Additional_Information/ws:Book_on_AV='1' or
ws:Chapter2/ws:Has_International_Assignment='false']/ws:Chapter3/ws:Book_Type/text()"
for book in xml1.findall("ws:Book", namespaces=NAMESPACE1):
print(book.xpath(xp, namespaces=NAMESPACE1))
ERROR:
print(book.xpath(xp, namespaces=NAMESPACE1))
File "src\lxml\etree.pyx", line 1597, in lxml.etree._Element.xpath
File "src\lxml\xpath.pxi", line 305, in lxml.etree.XPathElementEvaluator.__call__
File "src\lxml\xpath.pxi", line 225, in lxml.etree._XPathEvaluatorBase._handle_result
lxml.etree.XPathEvalError: Invalid expression
File:
<?xml version="1.0" encoding="UTF-8"?>
<ws:Books xmlns:ws="urn:com.aa/bb" >
<ws:Book>enter code here
<ws:Chapter1>
<ws:Data>
<ws:Type>Preferred</ws:Type>
<ws:Book_Name>Book1</ws:Book_Name>
</ws:Data>
</ws:Chapter1>
<ws:Chapter2>
<ws:Has_International_Assignment>true</ws:Has_International_Assignment>
</ws:Chapter2>
<ws:Chapter3>
<ws:Operation>ADD</ws:Operation>
<ws:Book_Type ws:PriorValue="">Regular</ws:Book_Type>
</ws:Chapter3>
<ws:Additional_Information>
<ws:Book_on_AV>1</ws:Book_on_AV>
</ws:Additional_Information>
</ws:Book>
<ws:Book>
<ws:Chapter1>
<ws:Data>
<ws:Type>Preferred</ws:Type>
<ws:Book_Name>Book2</ws:Book_Name>
</ws:Data>
</ws:Chapter1>
<ws:Chapter2>
<ws:Has_International_Assignment>true</ws:Has_International_Assignment>
</ws:Chapter2>
<ws:Chapter3>
<ws:Operation>ADD</ws:Operation>
<ws:Book_Type ws:PriorValue="">Regular</ws:Book_Type>
</ws:Chapter3>
<ws:Additional_Information>
<ws:Book_on_AV>1</ws:Book_on_AV>
</ws:Additional_Information>
</ws:Book>
</ws:Books>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法使用方括号中的谓词启动路径表达式,即以
[
不起作用的内部XPath表达式。我希望使用。[
而不是使用LXML/XPATH 1.0测试)。如果。[
给出错误,请尝试self :: WS:book [
。You can't start a path expression with a predicate in square brackets, i.e. your inner XPath expression starting with
[
doesn't work. I would hope that using.[
instead works (but I haven't tested with lxml/XPath 1.0). If the.[
gives an error, tryself::ws:Book[
.