Read, write, modify and query XML and HTML from Ruby
open sourceMITself-hostablefree
Nokogiri (鋸) is the Ruby library for reading, writing, modifying and querying XML and HTML documents. It is a thin layer over native parsers (libxml2 and libxslt, libgumbo for HTML5, and xerces on JRuby), with DOM parsers for XML, HTML4 and HTML5, SAX and push parsers for XML and HTML4, search by XPath 1.0 or CSS3 selectors with some jQuery-like extensions, XSD schema validation, XSLT transforms and a Builder DSL for writing documents. It treats every document as untrusted by default. Precompiled native gems for Linux (glibc and musl), macOS, Windows and JRuby install in seconds without a C toolchain. It is MIT-licensed and maintained by Mike Dalessio, Aaron Patterson and other contributors, with commercial support through Tidelift.
Problems it solves
- parse an HTML page in Ruby and pull out elements with CSS selectors
- scrape a website from a Ruby script
- query an XML document with XPath in Ruby
- parse HTML5 the way browsers do from Ruby
- stream through a huge XML file in Ruby without loading it all into memory
- validate an XML document against an XSD schema in Ruby
- transform XML with an XSLT stylesheet from Ruby
- build an XML document with a Ruby DSL
- modify an HTML fragment safely before rendering it
- install an XML parser gem without compiling libxml2
Features
- DOM parser for XML, HTML4 and HTML5
- SAX parser for XML and HTML4
- push parser for XML and HTML4
- document search via XPath 1.0
- document search via CSS3 selectors, with some jQuery-like extensions
- XSD schema validation
- XSLT transformation
- Builder DSL for XML and HTML documents
- treats documents as untrusted by default
- precompiled native gems for x86_64, aarch64 and arm Linux (glibc and musl), x86_64 and arm64 macOS, x64 Windows (UCRT) and JRuby 10+
Limitations
- Behaves differently by parser: it stays a thin layer over libxml2, libgumbo and xerces and doesn't paper over their differences, so CRuby and JRuby can give different results1
- Searches with XPath 1.0 only: it doesn't support XPath 2.0 or 3.x1
- Needs a C toolchain off its native platforms: elsewhere the gem compiles its C extension and needs Ruby headers and system libraries1
Capabilities
| Has official SDKs | plan not known |
|---|
