ML is a flexible language for storing hierarchical data along with its associated metadata. Storing XML data enables you to manipulate and analyze it later. When writing applications, you probably use some kind of XSLT engine (Cocoon, for example) in order to pick out pieces of data from the structured XML file and transform them into an output format or process the data in some other way. Usually this means you have to write, compile, and then invoke code in order to test what it does on an XML file. Writing the code also usually requires one or two dozen lines of boilerplate code for proper compilation. This code could be Java (lots of extraneous boilerplate code) or XSLT (a little bit of extraneous boilerplate code). Wouldn't it be nice to just invoke the query and manipulation commands directly on the XML data without all the overhead?
Enter XSH, an open source command-line XML shell that lets you interactively query and manipulate XML data, simplifying development and testing of XML query and manipulation code. Since XSH is written in Perl, its syntax resembles Perl syntax, a boon if you are already familiar with Perl. XSH even lets you write Perl code and access XML data structures as if they were ordinary Perl variables. In addition to Perl, XSH gives you XSLT capabilities such as XPath querying, enabling you to quickly and succinctly express complex queries with the XPath language.
Perform the following steps to continue:
- Download XSH. You need both XML-XSH2-2.0.2.tar.gz (or later) and xmltools-bundle.tar.gz.
- Unpack both files (tar xzf $file.tar.gz).
- Type "su"
- Type "cd xmltools-bundle"
- Type "./install –s"
- Type "cd ../XML-XSH2-2.0.2"
- Type "perl Makefile.PL"
- Type "make"
- Type "make install"
- Hit Ctrl-D to exit the su shell and return to your original username.
Test that you have XSH installed by typing xsh --version
. You should see the following response:
Quick Test Drive
xsh 0.12 (Revision: 2.2)
XML::XSH2::Functions 2.0.2 (Revision: 2.7)
Briefly explore some XSH features by trying out a few commands on an example file. First download the sample code
for this article. Unpack it by typing tar xzf xsh_article_sample_code.tgz
. Proceed to type the following:
- cd xsh_article_sample_code
- $a := open "example1.xml"
- ls $a
You should see the XML code of example1.xml
Now, type the following:
XSH should respond with the following:
<title>The Picture of Dorian Gray</title>
 The studio was filled with the rich odor of roses, and when the
light summer wind stirred amidst the trees of the garden there came
through the open door the heavy scent of the lilac, or the more
delicate perfume of the pink-flowering thorn.
The ellipsis (...) is my abbreviation for the content I omitted from the actual XSH listing. I just issued an XPath query for all books whose authors' last names are Wilde. I created the example1.xml
file to contain exactly five books, one of which is The Picture of Dorian Gray
, written by Oscar Wilde. The following XSH command will show all of the books in the example XML file:
That should yield the following:
<title>All Quiet on the Western Front</title>
<title>A Separate Peace</title>
<title>The Call of the Wild</title>
<title>The Picture of Dorian Gray</title>
To see all authors, issue the following command:
You can issue any XPath query, meaning you can search by tag name, tag content, attribute values, and anything else specified in the XPath standard. The rich searching capability of XPath is one of the key benefits of storing data in XML format. Try a few (or all) of the following commands to get a flavor for the types of queries you can issue:
- ls //book[substring-before(publication-date, "-") > 1996]
- ls //book[contains(chapters/chapter/title, "Primitive")]
- ls //book[contains(title, "Dorian")]
- ls //title[starts-with(., "Oliver")]
- count(//publisher[contains(., "Books")])
- ls //book[contains(publisher, "Books")]/chapters/chapter
For more information on XPath and how to use it, consult the official specification
and an excellent XPath Tutorial
. You can follow along with the examples in the left frame of the tutorial since all of them should work in XSH.