Wildcard namespaces in lxml

2024/10/13 19:24:23

How to query using xpath ignoring the xml namespace? I am using python lxml library. I tried the solution from this question but doesn't seem to work.

In [151]: e.find("./*[local-name()='Buckets']")File "<string>", line unknown
SyntaxError: invalid predicate
Answer

Use e.xpath, not e.find:


import lxml.etree as ETcontent = '''\
<Envelope xmlns="http://www.example.com/zzz/yyy"><Header><Version>1</Version></Header><Buckets>some stuff</Buckets>
</Envelope>
'''
root = ET.fromstring(content)
print(root.xpath("./*[local-name()='Buckets']"))
# [<Element {http://www.example.com/zzz/yyy}Buckets at 0xb73a62ac>]
https://en.xdnf.cn/q/69497.html

Related Q&A

WordNet - What does n and the number represent?

My question is related to WordNet Interface.>>> wn.synsets(cat)[Synset(cat.n.01), Synset(guy.n.01), Synset(cat.n.03),Synset(kat.n.01), Synset(cat-o-nine-tails.n.01), Synset(caterpillar.n.02), …

How to change the values of a column based on two conditions in Python

I have a dataset where I have the time in a game and the time of an event. EVENT GAME0:34 0:43NaN 0:232:34 3:43NaN 4:50I want to replace the NaN in the EVENT column where GAME…

logging module for python reports incorrect timezone under cygwin

I am running python script that uses logging module under cygwin on Windows 7. The date command reports correct time:$ date Tue, Aug 14, 2012 2:47:49 PMHowever, the python script is five hours off:201…

Set ordering of Apps and models in Django admin dashboard

By default, the Django admin dashboard looks like this for me:I want to change the ordering of models in Profile section, so by using codes from here and here I was able to change the ordering of model…

python database / sql programming - where to start

What is the best way to use an embedded database, say sqlite in Python:Should be small footprint. Im only needing few thousands records per table. And just a handful of tables per database. If its one …

How to install Python 3.5 on Raspbian Jessie

I need to install Python 3.5+ on Rasbian (Debian for the Raspberry Pi). Currently only version 3.4 is supported. For the sources I want to compile I have to install:sudo apt-get install -y python3 pyth…

Django - last insert id

I cant get the last insert id like I usually do and Im not sure why.In my view:comment = Comments( ...) comment.save() comment.id #returns NoneIn my Model:class Comments(models.Model):id = models.Integ…

How to check if default value for python function argument is set using inspect?

Im trying to identify the parameters of a function for which default values are not set. Im using inspect.signature(func).parameters.value() function which gives a list of function parameters. Since Im…

OpenCV-Python cv2.CV_CAP_PROP_POS_FRAMES error

Currently, I am using opencv 3.1.0, and I encountered the following error when executing the following code:post_frame = cap.get(cv2.CV_CAP_PROP_POS_FRAMES)I got the following error Message:File "…

How to get the total number of tests passed, failed and skipped from pytest

How can I get to the statistics information of a test session in pytest?Ive tried to define pytest_sessionfinish in the conftest.py file, but I only see testsfailed and testscollected attributes on th…