Skip to content

Instantly share code, notes, and snippets.

@mikevm
Created July 7, 2016 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikevm/71095581645c62a971194b3142d09b92 to your computer and use it in GitHub Desktop.
Save mikevm/71095581645c62a971194b3142d09b92 to your computer and use it in GitHub Desktop.
require 'nokogiri'
def create_xml_doc_with_syntax_error_on_line(line_no)
doc = []
doc << '<?xml version="1.0" encoding="UTF-8"?>'
doc << '<testdoc>'
1e5.to_i.times do
if line_no == (doc.size + 1)
doc << '<item>b</item>'
else
doc << '<item>a</item>'
end
end
doc << '</testdoc>'
doc.join("\n");
end
schema_definition_file = <<-'SCHEMA'
<grammar xmlns="http://relaxng.org/ns/structure/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
<define name="item-elements">
<data type="string">
<param name="pattern">a</param>
</data>
</define>
<define name="catalog-root">
<element>
<anyName/>
<oneOrMore>
<element name="item">
<ref name="item-elements"></ref>
</element>
</oneOrMore>
</element>
</define>
<start>
<ref name="catalog-root" />
</start>
</grammar>
SCHEMA
schema_definition = Nokogiri::XML::RelaxNG(schema_definition_file)
puts "Nokogiri: #{Gem.loaded_specs['nokogiri'].version}"
[4e2, 4e4, 7e4].each do |error_line_no|
puts sprintf('XML document with a schema violation on line %d', error_line_no)
xml_doc = Nokogiri.XML(create_xml_doc_with_syntax_error_on_line(error_line_no))
schema_definition.validate(xml_doc).each_with_index do |error, i|
puts sprintf(' %d. RelaxNG reports error on line %d: %s', i, error.line, error.message)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment