Ruby XML, XSLT and XPath tutorials

What is XML?

Ruby provides two levels of access to the network, and at the bottom you can access the operating system, which allows you to implement basic socket support for both client and server for connection-oriented and connectionless protocols.

Ruby unified support for application network protocols such as FTP, HTTP, and more.

Whether it is high-level or low-level. Ruby provides some basic classes that allow you to interact with many protocols, such as TCP, UDP, SOCKS, etc., without having to stick to the network layer. These classes also provide helper classes that allow you to easily read and write to the server.

Next let's learn how to do Ruby Socket programming

What is Sockets

When the application layer communicates data through the transport layer, TCP and UDP encounter the problem of providing concurrent services for multiple application processes at the same time. Multiple TCP connections or multiple application processes may need to transfer data over the same TCP protocol port. To distinguish between different application processes and connections, many computer operating systems provide an interface called Sockets for applications to interact with the TCP/IP protocol, distinguishing between network communications and connections between different application processes.

The generated socket has three main parameters: the destination IP address of the communication, the transport layer protocol used (TCP or UDP), and the port number used. The original meaning of Socket is "socket". By combining these three parameters and binding to a "socket" socket, the application layer can communicate with the transport layer through a socket interface to distinguish communication from different application processes or network connections, and implement concurrent services for data transmission.

Sockets vocabulary analysis:

domain indicates which protocol family to use, usually PF_INET, PF_UNIX, PF_X25, and so on.
typeSpecify the type of socket: SOCK_STREAM or SOCK_DGRAM. The Socket interface also defines the original Socket (SOCK_RAW), allowing the program to use low-level protocols
protocol is usually assigned a value of 0.
hostnameNetwork interface identifier:
  • String, which can be a hostname or IP address
  • String "<broadcast>", specifies the INADDR_BROADCAST address.
  • 0 length string specifying INADDR_ANY
  • An integer that is interpreted as the binary address of the host byte order.
portport is the port number, each server will listen to one or more port numbers of the client connection, a port number can be the port number of Fixnum, including the server name and port.

Simple client

The following we have written a simple client instance for a given host and port. The Ruby TCPSocket class provides an open method to open a socket.

TCPSocket.open(hosname, port ) Open a TCP connection.

Once you open a Socket connection, you can read it like an IO object. When you're done, you need to close the connection as if you were closing the file.

The following example demonstrates how to connect to a specific host, read data from the socket, and finally close the socket:

<collection shelf=" New Arrivals"> <movie title="Enemy Behind"> <type>War, Thriller</type > <format>DVD</format> <year>2003</year> <rating>PG</rating> <stars>10</stars> <description>Talk about a US-Japan war</description > </movie> <movie title="Transformers"> <type>Anime, Science Fiction</type> <format>DVD</format> <year>1989</year> <rating>R</rating> <stars>8</stars> <description>A schientific fiction</description > </movie> <movie title="Trigun"> <type>Anime, Action</type > <format>DVD</format> <episodes>4</episodes> <rating>PG</rating> <stars>10</stars> <description>Vash the Stampede!</description> </movie> <movie title="Ishtar"> <type>Comedy</type> <format>VHS</format> <rating>PG</rating> <stars>2</stars> <description>Viewable boredom</description> </movie> </collection>

DOM parser

Let's first parse the XML data. First we introduce the rexml/document library. Usually we can introduce REXML in the top-level namespace:


#!/usr/bin/ruby -w require 'rexml/document' include REXML xmlfile= File.new("movies.xml") xmldoc = Document.new(xmlfile) # Get the root element root= xmldoc.root puts "Root element : " + root.attributes ["shelf" ] # The following will output the movie title xmldoc.elements.each("collection/movie"){ |e| puts "Movie Title : " + e.attributes["title"] } # All movie types will be output below< /span> xmldoc.elements.each("collection/movie/type") { |e| puts "Movie Type : " + e.text } # All movie descriptions will be output below< /span> xmldoc.elements.each("collection/movie/description") { |e| puts "Movie Description : " + e.text }

The output of the above example is:

Root element : New Arrivals
Movie Title : Enemy Behind
Movie Title : Transformers
Movie Title : Trigun
Movie Title : Ishtar
Movie Type : War , Thriller
Movie Type : Anime , Science  Fiction
Movie Type : Anime , Action
Movie Type : Comedy
Movie Description : Talk About a US-Japan war
Movie Description : A schientific fiction
Movie Description : Vash The Stampede!
Movie Description : Viewable Boredom
SAX-like Parsing:

SAX parser

Process the same data file: movies.xml. It is not recommended to parse SAX into a small file. Here is a simple example:


#!/usr/bin/ruby -w require 'rexml/document' require 'rexml/streamlistener' include REXML class MyListener include REXML::StreamListener def tag_start(*args) puts "tag_start: #{args.map {|x| x.inspect}.join(', ')}" end def text(data) return if data =~ /^\w* $/ # whitespace only abbrev = data[0..40] + (data.length > 40 ? "..." : "") puts " text : #{abbrev.inspect}" end end list= MyListener.new xmlfile= File.new("movies.xml") Document.parse_stream(xmlfile, list)

The above output is:

tag_start: "collection", { "shelf"=>"New Arrivals" }
Tag_start: "movie", {"title"=>"Enemy Behind"}
Tag_start: "type", {}
  Text : "War, Thriller"
Tag_start: "format", {}
Tag_start: "year", {}
Tag_start: "rating", {}
Tag_start: "stars", {}
Tag_start: "description", {}
  Text : "Talk about a US-Japan war"
Tag_start: "movie", {"title"=>"Transformers"}
Tag_start: "type", {}
  Text : "Anime, Science Fiction"
Tag_start: "format", {}
Tag_start: "year", {}
Tag_start: "rating", {}
Tag_start: "stars", {}
Tag_start: "description", {}
  Text : "A schientific fiction"
Tag_start: "movie", {"title"=>"Trigun"}
Tag_start: "type", {}
  Text : "Anime, Action"
Tag_start: "format", {}
Tag_start: "episodes", {}
XPath.each(xmldoc, "//type") { |e| puts e.text }
# Get the types of all movie formats, return an array
names= XPath.match(xmldoc, "/ /format").map {|x| x.text }
p names

The yield of the above model is:

<movie title='Enemy Behind'> ... </> 

War, Thriller 

Anime, Science Fiction 

Anime, Action 


["DVD", "DVD", "DVD", "VHS"]

XSLT and Ruby

There are two XSLT parsers in Ruby, which are quickly portrayed below:


This parser was composed and kept up by Justice Masayoshi Takahash. This is principally composed for the Linux working framework and requires the accompanying libraries:

  • Sablot
  • Iconv
  • Expat

You can discover these libraries at Ruby-Sablotron.


The XSLT4R was composed by Michael Neumann. XSLT4R is utilized for straightforward direction line cooperation and can be utilized by outsider applications to change XML reports.

XSLT4R requires XMLScan activities, including the XSLT4R chronicle, which is a 100% Ruby module. These modules can be introduced utilizing the standard Ruby establishment technique (ie Ruby install.rb).

The XSLT4R sentence structure is as follows:

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

If you need to utilize XSLT4R in your application, you can present XSLT and enter the parameters you need. A precedent is as follows:


require " xslt" stylesheet = File.readlines("stylesheet.xsl").to_s xml_doc= File.readlines("document.xml").to_s arguments = { ' image_dir' => '/....' } sheet = XSLT::Stylesheet.new( stylesheet , arguments ) # output to StdOut sheet.apply( xml_doc ) # output to 'str' str = "" sheet.output = [ str ] sheet.apply( xml_doc )

More information

welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.