Re: [xsl] cmdline XPath utility

Subject: Re: [xsl] cmdline XPath utility
From: Stephane Bortzmeyer <bortzmeyer@xxxxxx>
Date: Mon, 3 Nov 2003 14:25:41 +0100
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