|
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: cmdline XPath utility
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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|

Cart








