Ruby Tutorial
Ruby CGI
Ruby Advanced
Let's go over how you can work with XML, XSLT, and XPath in Ruby.
Ruby provides several libraries to work with XML. In this tutorial, we'll use Nokogiri, a popular gem that can read, write, and manipulate XML (and HTML) documents. It also provides support for XSLT transformations and XPath queries.
1. Installing Nokogiri
Before you start, you need to install the Nokogiri gem. You can do this by running:
gem install nokogiri
2. Parsing XML
You can create an XML document from a string or a file:
require 'nokogiri' # From a string xml_string = '<root><child>Hello, world!</child></root>' doc = Nokogiri::XML(xml_string) # From a file doc = Nokogiri::XML(File.open('document.xml'))
3. Querying XML with XPath
Once you have a Nokogiri::XML::Document
, you can use XPath to query the XML:
nodes = doc.xpath('//child') nodes.each do |node| puts node.text # Outputs: Hello, world! end
4. Modifying XML
You can also modify XML documents:
node = doc.at_xpath('//child') node.content = 'Goodbye, world!' puts doc.to_xml # Outputs: <?xml version="1.0"?>\n<root>\n <child>Goodbye, world!</child>\n</root>\n
5. Applying XSLT Stylesheets
Nokogiri supports XSLT transformations:
xslt_string = <<-XSLT <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/root"> <newRoot> <newChild><xsl:value-of select="child"/></newChild> </newRoot> </xsl:template> </xsl:stylesheet> XSLT xslt = Nokogiri::XSLT(xslt_string) new_doc = xslt.transform(doc) puts new_doc.to_xml # Outputs: <?xml version="1.0"?>\n<newRoot>\n <newChild>Goodbye, world!</newChild>\n</newRoot>\n
This is a basic introduction to working with XML, XSLT, and XPath in Ruby using Nokogiri. Depending on your use case, you might need to handle namespaces, errors, more complex XPath queries, or other XML features. Be sure to check the Nokogiri documentation for more details.
Parsing XML with Ruby:
Use the built-in REXML
library for XML parsing.
require 'rexml/document' xml_string = '<root><element>Value</element></root>' xml_document = REXML::Document.new(xml_string)
Creating XML documents in Ruby:
Build XML documents programmatically with REXML
.
require 'rexml/document' xml_document = REXML::Document.new root = xml_document.add_element('root') element = root.add_element('element') element.text = 'Value'
Ruby XML namespaces:
Work with XML namespaces using REXML
.
require 'rexml/document' xml_document = REXML::Document.new root = xml_document.add_element('root', 'xmlns:prefix' => 'http://example.com')
Transforming XML with XSLT in Ruby:
Apply XSLT transformations using LibXSLT
.
require 'libxslt' xslt = LibXSLT::XSLT.parse(File.read('transform.xslt')) result = xslt.apply(xml_document)
Applying XSLT stylesheets in Ruby:
Apply XSLT stylesheets using REXML
.
require 'rexml/document' require 'rexml/xslt' xml_document = REXML::Document.new(xml_string) xslt_stylesheet = REXML::Document.new(File.read('transform.xslt')) result = REXML::XSLT.new(xslt_stylesheet).apply(xml_document)
Using XPath with Ruby: Navigate XML documents using XPath.
require 'rexml/document' xml_document = REXML::Document.new(xml_string) element_value = REXML::XPath.first(xml_document, '//element').text
Ruby XPath examples:
Perform XPath queries with REXML
.
require 'rexml/document' xml_document = REXML::Document.new(xml_string) elements = REXML::XPath.match(xml_document, '//element')
Searching and querying XML in Ruby: Use various methods to search and query XML documents.
require 'nokogiri' xml_document = Nokogiri::XML(xml_string) element_value = xml_document.at('//element').text
Ruby XML validation:
Validate XML against a schema or DTD using Nokogiri
.
require 'nokogiri' xml_document = Nokogiri::XML(xml_string) schema = Nokogiri::XML::RelaxNG(File.read('schema.rng')) validation_result = schema.valid?(xml_document)
Ruby XML libraries comparison:
Compare XML libraries like REXML
, LibXML
, and Nokogiri
.
# Choose the appropriate library based on your requirements
Processing XML with Nokogiri in Ruby:
Utilize the powerful Nokogiri
library for XML processing.
require 'nokogiri' xml_document = Nokogiri::XML(xml_string)
Working with RSS or Atom feeds in Ruby: Parse and manipulate RSS or Atom feeds using relevant libraries.
require 'rss' rss_feed = RSS::Parser.parse(rss_xml)