Ruby XPath and XSLT

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Ruby XPath and XSLT

Ruby XPath

Ruby XPath is a language to find information in an XML file. It is an alternative to view XML file. It is used to navigate through elements and attributes in an XML document, treating that document as a logical ordered tree.

Ruby XPath is very useful to get relevant information and attributes from XML file. It takes tree-based parsing.


  1. #!/usr/bin/ruby -w   
  3. require 'rexml/document'   
  4. include REXML   
  6. xmlfile ="trial.xml")   
  7. xmldoc =   
  9. # Info for the first cloth found   
  10. clothing = XPath.first(xmldoc, "//clothing")   
  11. p clothing   
  13. # Print out all the cloth types   
  14. XPath.each(xmldoc, "//type") { |e| puts e.text }   
  16. # Get an array of all of the cloth brands.   
  17. names = XPath.match(xmldoc, "//brand").map {|x| x.text }   
  18. p names  


Ruby Xpath and xslt 1


Ruby XSLT is a simple class based on libxml and libxslt. There are two XSLT parsers available for Ruby.

  • Ruby-Sablotron
  • XSLT4R

It is freely distributable according to the terms of GNU. This program is distributed without any warranty.


It is mainly written for Linux operating system and is written by Masayoshi Takahashi.

It requires the following libraries:

  • Sablot
  • Iconv
  • Expat


It is written by Michael Neumann. It uses a simple command line interface and it can be alternatively used within a third-party application to transform an XML document.

XSLT4R needs XMLScan to operate, which is included within the XSLT4R archieve. These modules need to be installed using standard Ruby installation method.


ruby xslt.rb stylesheet.xsl document.xml [arguments] 

To use XSLT4R within an application, you need to include XSLT and input the parameters you need.