[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: cmdline XPath utility

Subject: Re: cmdline XPath utility
From: Stephane Bortzmeyer <bortzmeyer@xxxxxx>
Date: Mon, 3 Nov 2003 14:25:41 +0100
xpath reader
On Mon, Nov 03, 2003 at 11:59:14AM +0000,
 Richard Lewis <richard.lewis@xxxxxxxxx> wrote 
 a message of 11 lines which said:

> Does anyone know if theres a command line program that you can us to execute 
> XPath expressions on a given XML document and return the result to standard 
> output?

It's quite simple to write if you are not satisfied with one of the
several solutions mentioned. For the record, with Python, you can do
it as simply as:

#!/usr/bin/python

from xml.dom import ext
from xml.dom.ext.reader import PyExpat
from xml.xpath import Evaluate

if __name__ == '__main__':
    import sys
    if len(sys.argv) < 3:
      print "Usage: xpath expression file ..."
      sys.exit(1)
    expr = sys.argv[1]
    for fileName in sys.argv[2:]:
      #build a DOM tree from the file
      reader = PyExpat.Reader()
      xml_dom_object = reader.fromUri(fileName)
      retval = Evaluate(expr, xml_dom_object.documentElement)
      if len(retval):
          print "Document %s meets our criteria (\"%s\" found %d times)" % (fileName,expr,len(retval))
      else:
          print "Document %s does NOT meet our criteria (\"%s\" not found)" % (fileName,expr)
      #reclaim the object
      reader.releaseNode(xml_dom_object)

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.