LXML - .//{DAV:}response raises lxml.etree.XPathEvalError: Invalid expression
LXML - .//{DAV:}response raises lxml.etree.XPathEvalError: Invalid expression
我正在尝试使用以下方式获取所有 response
标签:
.//{DAV:}response
tree = etree.fromstring(r.content)
for response in tree.xpath(".//{DAV:}response"):
...
问题是lxml
returns这个错误:
lxml.etree.XPathEvalError: Invalid expression
我试图避免定义名称空间,因为每个 CardDav 服务器都有不同的名称空间(谁知道为什么)。
这个表达式有什么问题?
XML:
<?xml version='1.0' encoding='utf-8'?>
<multistatus xmlns="DAV:" xmlns:CR="urn:ietf:params:xml:ns:carddav" xmlns:CS="http://calendarserver.org/ns/">
<response>
<href>/admin/</href>
<propstat>
<prop>
<resourcetype>
<principal/>
<collection/>
</resourcetype>
</prop>
<status>HTTP/1.1 200 OK</status>
</propstat>
<propstat>
<prop>
<displayname/>
<CS:getctag/>
</prop>
<status>HTTP/1.1 404 Not Found</status>
</propstat>
</response>
<response>
<href>/admin/yyyyyy/</href>
<propstat>
<prop>
<resourcetype>
<CR:addressbook/>
<collection/>
</resourcetype>
<displayname>addressbook1</displayname>
<CS:getctag>"xxxxx"</CS:getctag>
</prop>
<status>HTTP/1.1 200 OK</status>
</propstat>
</response>
</multistatus>
考虑为默认名称空间下的解析节点分配一个临时前缀,该前缀应该适用于 xpath
和 findall
:
tree = etree.fromstring(r.content)
nsmp = {'doc': 'DAV:'}
for response in tree.xpath(".//doc:response", namespaces=nsmp):
...
for response in tree.findall(".//doc:response", namespaces=nsmp):
...
我正在尝试使用以下方式获取所有 response
标签:
.//{DAV:}response
tree = etree.fromstring(r.content)
for response in tree.xpath(".//{DAV:}response"):
...
问题是lxml
returns这个错误:
lxml.etree.XPathEvalError: Invalid expression
我试图避免定义名称空间,因为每个 CardDav 服务器都有不同的名称空间(谁知道为什么)。
这个表达式有什么问题?
XML:
<?xml version='1.0' encoding='utf-8'?>
<multistatus xmlns="DAV:" xmlns:CR="urn:ietf:params:xml:ns:carddav" xmlns:CS="http://calendarserver.org/ns/">
<response>
<href>/admin/</href>
<propstat>
<prop>
<resourcetype>
<principal/>
<collection/>
</resourcetype>
</prop>
<status>HTTP/1.1 200 OK</status>
</propstat>
<propstat>
<prop>
<displayname/>
<CS:getctag/>
</prop>
<status>HTTP/1.1 404 Not Found</status>
</propstat>
</response>
<response>
<href>/admin/yyyyyy/</href>
<propstat>
<prop>
<resourcetype>
<CR:addressbook/>
<collection/>
</resourcetype>
<displayname>addressbook1</displayname>
<CS:getctag>"xxxxx"</CS:getctag>
</prop>
<status>HTTP/1.1 200 OK</status>
</propstat>
</response>
</multistatus>
考虑为默认名称空间下的解析节点分配一个临时前缀,该前缀应该适用于 xpath
和 findall
:
tree = etree.fromstring(r.content)
nsmp = {'doc': 'DAV:'}
for response in tree.xpath(".//doc:response", namespaces=nsmp):
...
for response in tree.findall(".//doc:response", namespaces=nsmp):
...