473,574 Members | 2,510 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ElementTree and XPATH

Hi,

I'm using ElementTree from effbot (http://effbot.org/zone/element.htm)
and I'm having some problems finding nodes that have the same name. I
know in XPATH, we can use an index to identify which node we need, but
it seems to be invalid syntax if I give "/a/b[0]" to the findall()
method. Does anyone know the correct syntax?
Any help will be much appreciated.

Cheers,
Michael

Jul 18 '05 #1
3 2409
<da*****@hotmai l.com> wrote:
I'm using ElementTree from effbot (http://effbot.org/zone/element.htm)
and I'm having some problems finding nodes that have the same name. I
know in XPATH, we can use an index to identify which node we need, but
it seems to be invalid syntax if I give "/a/b[0]" to the findall()
method. Does anyone know the correct syntax?


the XPath subset supported by ElementTree is documented here:

http://effbot.org/zone/element-xpath.htm

for more extensive support, see Ken Rimey's PDIS toolkit:

http://pdis.hiit.fi/pdis/download/

</F>

Jul 18 '05 #2
da*****@hotmail .com wrote:
it seems to be invalid syntax if I give "/a/b[0]" to the findall()
method. Does anyone know the correct syntax?


I think the proper mindset going in should be that
elementtree does not support xpath but that
there are some handy constructs that resemble
the location steps of xpath.

Sometimes it takes very little work to achieve what
you want directly with python. In your case you could
probably use:

findall("/a/b")[0]

to the same effect.

Istvan.
Jul 18 '05 #3
Istvan Albert wrote:
da*****@hotmail .com wrote:
it seems to be invalid syntax if I give "/a/b[0]" to the findall()
method. Does anyone know the correct syntax?

I think the proper mindset going in should be that
elementtree does not support xpath but that
there are some handy constructs that resemble
the location steps of xpath.


The lxml Pythonic wrapper of libxml2 which aims (among others) to build
an elementtree API compatible interface will indeed extend that API and
offer XPath support. Of course it's all not done yet. :)

http://codespeak.net/mailman/listinfo/lxml-dev
http://codespeak.net/svn/lxml/trunk/

Regards,

Martijn
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
10074
by: Chris Spencer | last post by:
Does anyone know how to make ElementTree preserve namespace prefixes in parsed xml files? The default behavior is to strip a document of all prefixes and then replace them autogenerated prefixes like ns0, ns1, etc. The correct behavior should be to write the file in the form that it was read, which it seems to do correctly for everything...
1
2084
by: Gerard Flanagan | last post by:
Pseudo-XPath support for ElementTree with the emphasis on 'Pseudo'. http://gflanagan.net/site/python/pagliacci/ElementFilter.html It's an approach suggested by the Specification Pattern eg. http://www.martinfowler.com/apsupp/spec.pdf Not tested beyond what follows (no time for more at the minute).
2
3021
by: marc.wyburn | last post by:
I'm playing with XML and elementtree and am missing something but I'm not sure what...? I've create an XML file with Elementtree with a root of backup.xml. Attached to the root is a dirob and the dirobj has a fileobj. fileobj has filename and filesize tags. I can open the file in excel and it sets out the columns as I would expect. The...
30
4602
by: Chas Emerick | last post by:
I looked around for an ElementTree-specific mailing list, but found none -- my apologies if this is too broad a forum for this question. I've been using the lxml variant of the ElementTree API, which I understand works in much the same way (with some significant additions). In particular, it shares the use of a .tail attribute. I ran...
11
3512
by: Peter Pei | last post by:
One bad design about elementtree is that it has different ways parsing a string and a file, even worse they return different objects: 1) When you parse a file, you can simply call parse, which returns a elementtree, on which you can then apply xpath; 2) To parse a string (xml section), you can call XML or fromstring, but both return element...
0
995
by: Andrew Lonie | last post by:
Hi I noticed that the xpath functionality of elementtree has been upgraded in version 1.3. However I can't seem to get the predicate to function. All the other new functionality seems to be working. Maybe I'm getting the syntax wrong?: att="2">text2</tag></root>""")
1
294
by: Mike Slinn | last post by:
The following short Python program parses a KML file and displays the names of all Marks and Routes: from elementtree.ElementTree import ElementTree tree = ElementTree(file='test.kml') kml = tree.getroot() ns = 'http://earth.google.com/kml/2.1' for folder in kml.findall("{%s}Folder/{%s}Folder/{%s}name" % (ns, ns, ns)): print folder.text
4
1749
by: jaime.dyson | last post by:
Hello all, I have the unenviable task of turning about 20K strangely formatted XML documents from different sources into something resembling a clean, standard, uniform format. I like Elementtree and have been using it to step through the documents to get a feel for their structure. .getiterator() gives me a depth-first traversal that...
1
5767
by: xkenneth | last post by:
All, Can I execute XPath queries on ElementTree objects ignoring the namespace? IE './node' instead of './{http://namespace.com}node'. Is there any support for XPath and Minidom? Regards, Ken
0
7794
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7710
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8040
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8225
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7800
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8092
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5610
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5296
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.