Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
XPath and XQuery in Python?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Nelson Minar  
View profile  
 More options Jan 11 2005, 7:09 pm
Newsgroups: comp.lang.python
From: Nelson Minar <nel...@monkey.org>
Date: Wed, 12 Jan 2005 00:09:58 GMT
Local: Tues, Jan 11 2005 7:09 pm
Subject: XPath and XQuery in Python?
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Lenton  
View profile  
 More options Jan 13 2005, 6:39 pm
Newsgroups: comp.lang.python
From: John Lenton <j...@grulic.org.ar>
Date: Thu, 13 Jan 2005 20:39:00 -0300
Subject: Re: XPath and XQuery in Python?

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 (j...@grulic.org.ar) -- Random fortune:
The whole world is a scab.  The point is to pick it constructively.
                -- Peter Beard

  signature.asc
< 1K Download

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nelson Minar  
View profile  
 More options Jan 14 2005, 10:51 am
Newsgroups: comp.lang.python
From: Nelson Minar <nel...@monkey.org>
Date: Fri, 14 Jan 2005 15:51:52 GMT
Local: Fri, Jan 14 2005 10:51 am
Subject: Re: XPath and XQuery in Python?

Nelson Minar <nel...@monkey.org> 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/weblog/tech/python/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/weblog/tech/python/xpath.html


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Uche Ogbuji  
View profile  
 More options Jan 15 2005, 1:07 am
Newsgroups: comp.lang.python
From: "Uche Ogbuji" <u...@ogbuji.net>
Date: 14 Jan 2005 22:07:06 -0800
Local: Sat, Jan 15 2005 1:07 am
Subject: Re: XPath and XQuery in Python?
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »