« I Used to Hate Timesheets | Main | Award winning shareware? »
Command line xpath script
January 10, 2007
Here's a script I've been using to run XPath queries from the command line. It's handy for unit testing. If your code does a lot of XPath stuff, it'll be useful to unit test the query without running the whole program. The script shown here is simple, free, and only requires the free Ruby runtime.
# Syntax: xapth.rb <filename> "xpath query goes here"
# This code is based on an example from: _Ruby Cookbook_, "Navigating a Document with XPath"
require 'rexml/document'
# Get command line params
filename = ARGV[0]
xpathqry = ARGV[1]
# Read in XML file
xmlfile = ''
File.open(filename, "r") { |f| xmlfile = f.read }
# Create Xml Document from file contents
doc = REXML::Document.new xmlfile
puts "Using XPATH=" + xpathqry
# Run xpath query on XML document, and print the results
REXML::XPath.each(doc, xpathqry) { |val| puts "#{val}" }
Save the above script to a file called xpath.rb, and run like so:
With a little modification, this script can read from STDIN instead of a file, if you're into the whole piping/filtering thing.
January 10, 2007 at 12:00 PM in General | Permalink
TrackBack
TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d834205eab53ef00d83462edfa69e2
Listed below are links to weblogs that reference Command line xpath script:
