validate_xml_xsi 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/CHANGELOG.md +8 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +38 -0
- data/Rakefile +2 -0
- data/bin/validate_xml_xsi +15 -0
- data/lib/validate_xml_xsi.rb +66 -0
- data/validate_xml_xsi.gemspec +29 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ed79db97f53c99c86dff5c5acb649d7115dddd137ed5f91174aa12fe93187cf0
|
4
|
+
data.tar.gz: e6c379c63075f570f2f377f678476e410cbc5e0feb5a8091a4cc437b4b077877
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b8b464eff0d093ad0d3d7939045dcf3867f053f03966675a053a520af5786ebbef560584c17116bd2b0af7c101056772561621f6d1745404d3827a1e4807e85c
|
7
|
+
data.tar.gz: 32cf5298a1c8eca642701cec2966612fd3eabe17ed41ac92d531200d3f96a89a85d74a8a447ce4b0fc82209de200e0b5b92550f6d91ffa3e8264012475e4928a
|
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 David Hansen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# validate_xml_xsi
|
2
|
+
|
3
|
+
This gem validates XML against it's xsi using the xsi:schemaLocation elements to define the location of XSD documents.
|
4
|
+
|
5
|
+
It does this by first parsing the XML and searching for xsi:schemaLocation elements and then creating a schema document that imports the XSD's identified in the XML.
|
6
|
+
|
7
|
+
It then validates the document against that schema and outputs any error messages.
|
8
|
+
|
9
|
+
Note: this gem utilizes the 'nokogiri' gem for XML parsing and schema validation.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'validate_xml_xsi'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle install
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install validate_xml_xsi
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
validate_xml_xsi <xmlfiles>
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/d-hansen/validate_xml_xsi.
|
34
|
+
|
35
|
+
|
36
|
+
## License
|
37
|
+
|
38
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'validate_xml_xsi'
|
3
|
+
|
4
|
+
while ARGV.size > 0
|
5
|
+
fname = ARGV.shift
|
6
|
+
errors = XML::Schema::validate(fname)
|
7
|
+
if errors.empty?
|
8
|
+
puts "XML Schema check complete - NO ERRORS!"
|
9
|
+
else
|
10
|
+
puts "XML SCHEMA ERRORS FOUND:"
|
11
|
+
errors.each do |err|
|
12
|
+
puts err.description
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'nokogiri'
|
3
|
+
|
4
|
+
module XML
|
5
|
+
module Validation
|
6
|
+
class Error
|
7
|
+
Params = [:type, :file, :line, :column, :message, :err]
|
8
|
+
attr_reader *Params, :description
|
9
|
+
def initialize(*args)
|
10
|
+
Params.each.with_index { |param, idx| instance_variable_set("@#{param.to_s}", args[idx]) }
|
11
|
+
@description = "#{@type.to_s} ERROR [#{@file}:#{@line}:#{@column}]: #{@message}".freeze
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.parse(obj)
|
17
|
+
filename = nil
|
18
|
+
obj = File::read(filename = obj) if obj.is_a?(String) && File::exist?(obj)
|
19
|
+
xml_doc = Nokogiri::XML.parse(obj) { |cfg| cfg.strict.pedantic.nonet }
|
20
|
+
xml_doc.instance_variable_set('@filename', filename)
|
21
|
+
xml_doc.define_singleton_method(:filename) { instance_variable_get('@filename') }
|
22
|
+
xml_doc
|
23
|
+
end
|
24
|
+
|
25
|
+
module Schema
|
26
|
+
def self.validate(xml_doc)
|
27
|
+
xml_doc = XML::parse(xml_doc) unless xml_doc.is_a?(Nokogiri::XML::Document)
|
28
|
+
errors = []
|
29
|
+
## Build an all-in-one XSD document that imports all of the separate schema locations
|
30
|
+
xsd_doc = "<?xml version=\"1.0\"?>\n"
|
31
|
+
xsd_doc << "<xsd:schema targetNamespace=\"http://www.w3.orig/XML/1998/namespace\"\n" \
|
32
|
+
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" \
|
33
|
+
" version=\"1.0\">\n"
|
34
|
+
## Iterate over all the elements and find any xsi:schemaLocation attributes
|
35
|
+
## and build a hash of all of the namespaces and locations
|
36
|
+
schemata_by_ns = {}
|
37
|
+
xml_doc.search('//*[@xsi:schemaLocation]').each do |elem|
|
38
|
+
elem['xsi:schemaLocation'].scan(/(\S+)\s+(\S+)/).each do |ns_set|
|
39
|
+
if ns_loc = schemata_by_ns[ns_set.first]
|
40
|
+
unless ns_loc.eql?(ns_set.last)
|
41
|
+
error "MISMATCHING XMLNS XSI: #{ns_set.first} -> #{ns_loc} VS #{ns_set.last}"
|
42
|
+
end
|
43
|
+
else
|
44
|
+
schemata_by_ns[ns_set.first] = ns_set.last
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
schemata_by_ns.each do |ns_href, ns_file|
|
49
|
+
xsd_doc << " <xsd:import namespace=\"#{ns_href}\" schemaLocation=\"#{ns_file}\"/>\n"
|
50
|
+
end
|
51
|
+
xsd_doc << "</xsd:schema>\n"
|
52
|
+
xsd = Nokogiri::XML.Schema(xsd_doc)
|
53
|
+
xsd.errors.each do |err|
|
54
|
+
err_msg = (/ERROR: /.match(err.message)) ? $' : err.message
|
55
|
+
errors << XML::Validation::Error.new(:XSD, err.file, err.line, err.column, err_msg, err)
|
56
|
+
end
|
57
|
+
errs = xsd.validate(xml_doc)
|
58
|
+
errs.each do |err|
|
59
|
+
err_msg = (/ERROR: /.match(err.message)) ? $' : err.message
|
60
|
+
fname = (err.file.nil?) ? xml_doc.filename : err.file
|
61
|
+
errors << XML::Validation::Error.new(:XML, fname, err.line, err.column, err_msg, err)
|
62
|
+
end
|
63
|
+
errors
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "validate_xml_xsi"
|
3
|
+
spec.version = "0.1.2"
|
4
|
+
spec.authors = ["David Hansen"]
|
5
|
+
spec.email = ["david@hansen4.net"]
|
6
|
+
|
7
|
+
spec.summary = %q{Validate XML against it's embedded XSI elements that define the XSD's.}
|
8
|
+
spec.homepage = "https://github.com/d-hansen/validate_xml_xsi"
|
9
|
+
spec.license = "MIT"
|
10
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
11
|
+
|
12
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
13
|
+
spec.metadata["source_code_uri"] = "https://github.com/d-hansen/validate_xml_xsi"
|
14
|
+
spec.metadata["changelog_uri"] = "https://github.com/d-hansen/validate_xml_xsi/CHANGELOG.md"
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = "bin"
|
22
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 2.1"
|
26
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
27
|
+
|
28
|
+
spec.add_dependency "nokogiri", "~> 1.12.5"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validate_xml_xsi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Hansen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-12-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.1'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '13.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '13.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: nokogiri
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.12.5
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.12.5
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- david@hansen4.net
|
58
|
+
executables:
|
59
|
+
- validate_xml_xsi
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- CHANGELOG.md
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/validate_xml_xsi
|
70
|
+
- lib/validate_xml_xsi.rb
|
71
|
+
- validate_xml_xsi.gemspec
|
72
|
+
homepage: https://github.com/d-hansen/validate_xml_xsi
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata:
|
76
|
+
homepage_uri: https://github.com/d-hansen/validate_xml_xsi
|
77
|
+
source_code_uri: https://github.com/d-hansen/validate_xml_xsi
|
78
|
+
changelog_uri: https://github.com/d-hansen/validate_xml_xsi/CHANGELOG.md
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: 2.3.0
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubygems_version: 3.0.3
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Validate XML against it's embedded XSI elements that define the XSD's.
|
98
|
+
test_files: []
|