Skip to content

Instantly share code, notes, and snippets.

@ylansegal
Last active November 26, 2022 05:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ylansegal/eac88b27f5369b4f8ca7 to your computer and use it in GitHub Desktop.
Save ylansegal/eac88b27f5369b4f8ca7 to your computer and use it in GitHub Desktop.
Expose difference in behaviour in MRI vs JRuby
require 'nokogiri'
puts "Ruby: #{RUBY_DESCRIPTION}"
puts "Nokogiri: #{Nokogiri::VersionInfo.instance.to_hash['nokogiri']}"
puts
schema = <<-SCHEMA
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:element name="addresses">
<xs:complexType>
<xs:sequence>
<xs:element ref="address" minOccurs='1' maxOccurs='unbounded'/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="address">
<xs:complexType>
<xs:sequence>
<xs:element ref="name" minOccurs='0' maxOccurs='1'/>
<xs:element ref="street" minOccurs='0' maxOccurs='1'/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="name" type='xs:string'/>
<xs:element name="street" type='xs:string'/>
</xs:schema>
SCHEMA
bad_xml = <<-DOC
<?xml version="1.0" encoding="UTF-8"?>
<addresses xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation='test.xsd'>
<address>
<name>Joe Tester</name>
<street>Baker street 5</street>
<wrongExtraField/>
</wrongClosingTag>
</addresses>
DOC
good_xml = <<-DOC
<?xml version="1.0" encoding="UTF-8"?>
<addresses xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation='test.xsd'>
<address>
<name>Joe Tester</name>
<street>Baker street 5</street>
</wrongClosingTag>
</addresses>
DOC
xsd = Nokogiri::XML::Schema(schema)
puts "Validating good doc..."
xsd.validate(Nokogiri::XML(good_xml)).each do |error|
puts error.message
end
puts
puts "Validating bad doc..."
xsd.validate(Nokogiri::XML(bad_xml)).each do |error|
puts error.message
end
puts
puts "Validating good doc..."
xsd.validate(Nokogiri::XML(good_xml)).each do |error|
puts error.message
end
puts
$ ruby example.rb
Ruby: jruby 1.7.19 (1.9.3p551) 2015-01-29 20786bd on Java HotSpot(TM) 64-Bit Server VM 1.8.0_45-b14 [darwin-x86_64]
Nokogiri: 1.6.6.2

Validating good doc...

Validating bad doc...
cvc-complex-type.2.4.d: Invalid content was found starting with element 'wrongExtraField'. No child element is expected at this point.

Validating good doc...
cvc-complex-type.2.4.d: Invalid content was found starting with element 'wrongExtraField'. No child element is expected at this point.
# ^^^^^^--- This is unexpected to me!
$ ruby example.rb
Ruby: ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]
Nokogiri: 1.6.6.2

Validating good doc...

Validating bad doc...
Element 'wrongExtraField': This element is not expected.

Validating good doc...
# ^^^^^^--- This is what I expected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment