Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

XSH: Interactively Manipulate and Analyze XML Data

Most developers use some kind of XSLT engine to pick out and process data from structured XML files. Learn how XSH, an open source command-line XML shell, lets you interactively query and manipulate this data without the coding overhead.


advertisement
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.

Install XSH
Perform the following steps to continue:
  1. Download XSH. You need both XML-XSH2-2.0.2.tar.gz (or later) and xmltools-bundle.tar.gz.
  2. Unpack both files (tar xzf $file.tar.gz).
  3. Type "su"
  4. Type "cd xmltools-bundle"
  5. Type "./install –s"
  6. Type "cd ../XML-XSH2-2.0.2"
  7. Type "perl Makefile.PL"
  8. Type "make"
  9. Type "make install"
  10. 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:

xsh 0.12 (Revision: 2.2) XML::XSH2::Functions 2.0.2 (Revision: 2.7)

Quick Test Drive
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:
  1. cd xsh_article_sample_code
  2. xsh
  3. $a := open "example1.xml"
  4. ls $a
You should see the XML code of example1.xml.

Now, type the following:

ls //book[author/last-name="Wilde"]

XSH should respond with the following:


<book> <title>The Picture of Dorian Gray</title> <author> <first-name>Oscar</first-name> <middle-name/> <last-name>Wilde</last-name> </author> <publisher>Bantam Classics</publisher> <publication-date>1983-01-01</publication-date> <chapters> <chapter> <title>Chapter I</title> <content> [3] 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. </content> </chapter> ... </chapters> </book>

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:

ls //book/title

That should yield the following:

<title>All Quiet on the Western Front</title> <title>A Separate Peace</title> <title>Oliver Twist</title> <title>The Call of the Wild</title> <title>The Picture of Dorian Gray</title>

To see all authors, issue the following command:

ls //book/author

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.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap