Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > XPath and XQuery in Python?

Reply
Thread Tools

XPath and XQuery in Python?

 
 
Nelson Minar
Guest
Posts: n/a
 
      01-12-2005
Could someone help me get started using XPath or XQuery in Python? I'm
overwhelmed by all the various options and am lacking guidance on what
the simplest way to go is. What library do I need to enable three line
Python programs to extract data with XPath expressions?

I have this problem a lot with Python and XML. Even with Uche's
excellent yearly roundups I have a hard time finding how to do fancy
things with XML in Python. I think it's a bit like web server
frameworks in Python - too many choices.
http://www.xml.com/pub/a/2004/10/13/py-xml.html
 
Reply With Quote
 
 
 
 
John Lenton
Guest
Posts: n/a
 
      01-13-2005
On Wed, Jan 12, 2005 at 12:09:58AM +0000, Nelson Minar wrote:
> Could someone help me get started using XPath or XQuery in Python? I'm
> overwhelmed by all the various options and am lacking guidance on what
> the simplest way to go is. What library do I need to enable three line
> Python programs to extract data with XPath expressions?
>
> I have this problem a lot with Python and XML. Even with Uche's
> excellent yearly roundups I have a hard time finding how to do fancy
> things with XML in Python. I think it's a bit like web server
> frameworks in Python - too many choices.


my own favorite is libxml2. Something like the following:

#!/usr/bin/env python
import libxml2
import sys

def grep(what, where):
doc = libxml2.parseDoc(where)
for found in doc.xpathEval(what):
found.saveTo(sys.stdout, format=True)

if __name__ == '__main__':
try:
what = sys.argv[1]
except IndexError:
sys.exit("Usage: %s pattern file ..." % sys.argv[0])
else:
for where in sys.argv[2:]:
grep(what, file(where).read())

although you might want to be smarter with the errors...

--
John Lenton ((E-Mail Removed)) -- Random fortune:
The whole world is a scab. The point is to pick it constructively.
-- Peter Beard

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFB5waUgPqu395ykGsRAm/IAKCbwTqV86ZypBFbF9xBG2c3PVqzRQCfYTUb
+4+hVntBxGn86Qbzb6FsPPM=
=0gY7
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
 
Nelson Minar
Guest
Posts: n/a
 
      01-14-2005
Nelson Minar <(E-Mail Removed)> writes:
> Could someone help me get started using XPath or XQuery in Python?


I figured this out. Thanks for the help, John! Examples below.

I used this exercise as an opportunity to get something off my chest
about XML and Python - it's kind of a mess! More here:
http://www.nelson.monkey.org/~nelson...hon/xpath.html

Here are my samples, in three libraries:

# PyXML

from xml.dom.ext.reader import Sax2
from xml import xpath
doc = Sax2.FromXmlFile('foo.opml').documentElement
for url in xpath.Evaluate('//@xmlUrl', doc):
print url.value

# libxml2

import libxml2
doc = libxml2.parseFile('foo.opml')
for url in doc.xpathEval('//@xmlUrl'):
print url.content

# ElementTree

from elementtree import ElementTree
tree = ElementTree.parse("foo.opml")
for outline in tree.findall("//outline"):
print outline.get('xmlUrl')

Please see my blog entry for more commentary
http://www.nelson.monkey.org/~nelson...hon/xpath.html
 
Reply With Quote
 
Uche Ogbuji
Guest
Posts: n/a
 
      01-15-2005
Interesting discussion. My own thoughts:

http://www.oreillynet.com/pub/wlg/6224
http://www.oreillynet.com/pub/wlg/6225

Meanwhile, please don't make the mistake of bothering with XQuery.
It's despicable crap. And a huge impedance mismatch with Python.
--Uche

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xsdb better than RSS, RDF, XQuery, XPath, and XSLT most of the time ;) aaronwmail-usenet@yahoo.com XML 3 12-22-2004 10:02 PM
[ANN] Altova ships v2005 Products with support for XSLT 2.0, XPath 2.0, XQuery, Eclipse in XMLSpy, MapForce, StyleVision, Authentic Altova Announcements XML 1 11-17-2004 09:09 PM
when is xslt,xquery,xpath etc appropriate Jeff Kish XML 1 10-30-2004 02:01 PM
[ANN] Stylus Studio 6 Adds new Support for XSLT 2.0, XPath 2.0, XQuery,XML Schema, Legacy Data Integration and XML Mapping Stylus Studio XML 0 10-05-2004 06:17 PM
XQuery version of xpath different than xslt's? inquirydog XML 1 07-20-2004 08:21 AM



Advertisments