XPath and XQuery in Python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nelson Minar

    #1

    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.
    www.XML.com,Textuality Services,Uche Ogbuji,Programming, Community,The State of Python-XML in 2004

  • John Lenton

    #2
    Re: XPath and XQuery in Python?

    On Wed, Jan 12, 2005 at 12:09:58AM +0000, Nelson Minar wrote:[color=blue]
    > 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.[/color]

    my own favorite is libxml2. Something like the following:

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

    def grep(what, where):
    doc = libxml2.parseDo c(where)
    for found in doc.xpathEval(w hat):
    found.saveTo(sy s.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).rea d())

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

    --
    John Lenton (john@grulic.or g.ar) -- 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)

    iD8DBQFB5waUgPq u395ykGsRAm/IAKCbwTqV86ZypB FbF9xBG2c3PVqzR QCfYTUb
    +4+hVntBxGn86Qb zb6FsPPM=
    =0gY7
    -----END PGP SIGNATURE-----

    Comment

    • Nelson Minar

      #3
      Re: XPath and XQuery in Python?

      Nelson Minar <nelson@monkey. org> writes:[color=blue]
      > Could someone help me get started using XPath or XQuery in Python?[/color]

      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:


      Here are my samples, in three libraries:

      # PyXML

      from xml.dom.ext.rea der import Sax2
      from xml import xpath
      doc = Sax2.FromXmlFil e('foo.opml').d ocumentElement
      for url in xpath.Evaluate( '//@xmlUrl', doc):
      print url.value

      # libxml2

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

      # ElementTree

      from elementtree import ElementTree
      tree = ElementTree.par se("foo.opml")
      for outline in tree.findall("//outline"):
      print outline.get('xm lUrl')

      Please see my blog entry for more commentary

      Comment

      • Uche Ogbuji

        #4
        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

        Comment

        Working...