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.
Example:
#!/usr/bin/ruby -w
require 'rexml/document'
include REXML
xmlfile = File.new("trial.xml")
xmldoc = Document.new(xmlfile)
# Info for the first cloth found
clothing = XPath.first(xmldoc, "//clothing")
p clothing
# Print out all the cloth types
XPath.each(xmldoc, "//type") { |e| puts e.text }
# Get an array of all of the cloth brands.
names = XPath.match(xmldoc, "//brand").map {|x| x.text }
p names
Output:
Ruby XSLT
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.
Ruby-Sablotron
It is mainly written for Linux operating system and is written by Masayoshi Takahashi.
It requires the following libraries:
- Sablot
- Iconv
- Expat
XSLT4R
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.
Syntax:
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.
Leave a Reply