class Nokogiri::XML::ParseOptions

Options that control the parsing behavior for XML::Document, XML::DocumentFragment, HTML4::Document, HTML4::DocumentFragment, XSLT::Stylesheet, and XML::Schema.

These options directly expose libxml2’s parse options, which are all boolean in the sense that an option is “on” or “off”.

💡 Note that HTML5 parsing has a separate, orthogonal set of options due to the nature of the HTML5 specification. See Nokogiri::HTML5.

⚠ Not all parse options are supported on JRuby. Nokogiri will attempt to invoke the equivalent behavior in Xerces/NekoHTML on JRuby when it’s possible.

Setting and unsetting parse options

You can build your own combinations of parse options by using any of the following methods:

ParseOptions method chaining

Every option has an equivalent method in lowercase. You can chain these methods together to set various combinations.

# Set the HUGE & PEDANTIC options
po = Nokogiri::XML::ParseOptions.new.huge.pedantic
doc = Nokogiri::XML::Document.parse(xml, nil, nil, po)

Every option has an equivalent no{option} method in lowercase. You can call these methods on an instance of ParseOptions to unset the option.

# Set the HUGE & PEDANTIC options
po = Nokogiri::XML::ParseOptions.new.huge.pedantic

# later we want to modify the options
po.nohuge # Unset the HUGE option
po.nopedantic # Unset the PEDANTIC option

💡 Note that some options begin with “no” leading to the logical but perhaps unintuitive double negative:

po.nocdata # Set the NOCDATA parse option
po.nonocdata # Unset the NOCDATA parse option

💡 Note that negation is not available for STRICT, which is itself a negation of all other features.

Using Ruby Blocks

Most parsing methods will accept a block for configuration of parse options, and we recommend chaining the setter methods:

doc = Nokogiri::XML::Document.parse(xml) { |config| config.huge.pedantic }
ParseOptions constants

You can also use the constants declared under Nokogiri::XML::ParseOptions to set various combinations. They are bits in a bitmask, and so can be combined with bitwise operators:

po = Nokogiri::XML::ParseOptions.new(Nokogiri::XML::ParseOptions::HUGE | Nokogiri::XML::ParseOptions::PEDANTIC)
doc = Nokogiri::XML::Document.parse(xml, nil, nil, po)