Nokogiri is a Ruby gem for reading, writing, and querying XML and HTML. It wraps native parsers such as libxml2 and exposes DOM, SAX, XPath, CSS selectors, and validation helpers.
Overview
Nokogiri provides DOM, SAX, and push parsers for XML, HTML4, and HTML5 in Ruby applications. Security guidance treats untrusted documents carefully by default, and the API stays close to underlying parser behavior rather than papering over differences.
Query features include XPath 1.0, CSS3 selectors with jQuery-like extensions, XSD validation, XSLT transforms, and builder DSLs for generating markup. Strings are stored as UTF-8 internally while serialization respects source encodings.
CRuby builds ship native gems with bundled libxml2 and libxslt for faster installs on common Linux, macOS, and Windows platforms. JRuby uses a separate Java implementation backed by Xerces and related jars.
Ruby 3.2+ and JRuby 10+ are supported. Semantic versioning governs releases, with security updates typically shipped as patch versions.
Key features
- DOM, SAX, and push parsing
- XPath and CSS selector queries
- HTML4 and HTML5 support
- XSD validation and XSLT transforms
- Native gems with bundled libxml2
- JRuby and CRuby implementations
- Secure-by-default parsing stance
Use cases
- Scraping and sanitizing HTML in Ruby
- XML API clients
- Server-side markup generation