XPath and XQuery in Python?

Discussion in 'Python' started by Nelson Minar, Jan 12, 2005.

  1. Nelson Minar

    Nelson Minar Guest

    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
     
    Nelson Minar, Jan 12, 2005
    #1
    1. Advertising

  2. Nelson Minar

    John Lenton Guest

    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 () -- 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-----
     
    John Lenton, Jan 13, 2005
    #2
    1. Advertising

  3. Nelson Minar

    Nelson Minar Guest

    Nelson Minar <> 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
     
    Nelson Minar, Jan 14, 2005
    #3
  4. Nelson Minar

    Uche Ogbuji Guest

    Uche Ogbuji, Jan 15, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. inquirydog
    Replies:
    1
    Views:
    492
    Philippe Poulard
    Jul 20, 2004
  2. Stylus Studio
    Replies:
    0
    Views:
    544
    Stylus Studio
    Oct 5, 2004
  3. Jeff Kish
    Replies:
    1
    Views:
    394
    Tjerk Wolterink
    Oct 30, 2004
  4. Altova Announcements
    Replies:
    1
    Views:
    455
  5. Replies:
    3
    Views:
    776
    Andy Dingley
    Dec 22, 2004
Loading...

Share This Page