xsd 2.9.2 → 2.9.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 81b3091d973a5f08501a01ba6d6b746b4863bc28b5392ec634c4d226b7775a98
4
- data.tar.gz: 5bad6e4209bd42086a25d892a5c8a0845012ad91a57a97c7b80cf13f0656624b
3
+ metadata.gz: c17599aba21b245c4fd47315a81a25ff3241d5d8a5426f9d9b5a0d799c006d73
4
+ data.tar.gz: 22b50cc67c455af6e591e590b6692603f1346c0139c700084b53902c7dacb16f
5
5
  SHA512:
6
- metadata.gz: fd1216b66f6d84f386850e5eb8160eadac25677d739a5c140eb64a974f300c7e0eca26f7c0d5a1fcd9fd8bb665c16d06c91776ea57d13b322e1b925dce35f765
7
- data.tar.gz: 4a0145cebcb96eaf27b8a00fc7caf3be761a2d83b2809853f51c74c81702ae377168308a652ba716afd68fb2a7001b4a97ba713ea341c4983f11d14f8e007453
6
+ metadata.gz: 2d287545ee5c890c0d355f88c45ee839090d0ea03e9c747b43d6d6b52d1904e76b119d6737f77d971e6c45633664e71e875ce4993c3f760d761d8b833cac4311
7
+ data.tar.gz: f7a3db6bc00a00e44f88f1f9ecba06a0dc73bcabcb869ba37cb33980b067921239741384afc131fa210fbc84b23a4af57f725c82350d82861fcd2fc4528f9478
data/.rubocop.yml CHANGED
@@ -1,4 +1,4 @@
1
- require:
1
+ plugins:
2
2
  - rubocop-performance
3
3
  - rubocop-rake
4
4
  - rubocop-rspec
data/CHANGELOG.md CHANGED
@@ -1,3 +1,29 @@
1
+ ## [2.9.4] - 2026-10-26
2
+
3
+ ### Added
4
+
5
+ - RBS signatures in `sig/`: `xsd.rbs` from `rbs prototype rb` of `lib/`, and `xsd_members.rbs` with the readers that
6
+ `BaseObject.property`, `.child` and `.link` define for each class (`Element#name`, `#complex_type`, `#max_occurs`
7
+ as `Integer | :unbounded`, …), generated by `rake rbs:members` — run it after changing a class's members.
8
+
9
+ ## [2.9.3] - 2026-10-24
10
+
11
+ ### Fixed
12
+
13
+ - A schema with no target namespace that imports another namespace (an XMLDSig `Signature` referenced as
14
+ `ds:SignatureType`) now finds what it imports. `Schema#targets_namespace?` took a namespace URI for a prefix, looked
15
+ it up among the schema's prefixes, found nothing, and matched that nothing against the absent target namespace:
16
+ every reference by a prefixed name was looked for in the schema itself.
17
+
18
+ ### Changed
19
+
20
+ - Every import of the absent namespace is read from its own location. Schemas with no target namespace are each a
21
+ schema of their own (a provider's documents, every one shipping its own copies of the common types, which may
22
+ differ), so a second import of the absent namespace was not the same schema as the first one; before, it was
23
+ answered with whichever such schema had been loaded first. An import whose location is loaded already is answered
24
+ with that schema in any namespace (`XML#loaded_schema`); an import of a named namespace is still answered with a
25
+ schema known for it before its location is read.
26
+
1
27
  ## [2.9.2] - 2026-10-21
2
28
 
3
29
  ### Changed
data/Gemfile CHANGED
@@ -8,8 +8,8 @@ gemspec
8
8
  group :development do
9
9
  gem 'logger', '~> 1.5'
10
10
  gem 'rspec', '~> 3.12'
11
- gem 'rubocop', '~> 1.50'
12
- gem 'rubocop-performance', '~> 1.17'
13
- gem 'rubocop-rake', '~> 0.6'
14
- gem 'rubocop-rspec', '~> 2.10'
11
+ gem 'rubocop', '~> 1.92'
12
+ gem 'rubocop-performance', '~> 1.27'
13
+ gem 'rubocop-rake', '~> 0.7'
14
+ gem 'rubocop-rspec', '~> 3.8'
15
15
  end
data/Gemfile.lock ADDED
@@ -0,0 +1,89 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ xsd (2.9.4)
5
+ builder (~> 3.2)
6
+ nokogiri (~> 1.11)
7
+ rake (~> 13.0)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ ast (2.4.3)
13
+ builder (3.2.4)
14
+ diff-lcs (1.5.0)
15
+ json (3.1.0)
16
+ language_server-protocol (3.17.0.6)
17
+ lint_roller (1.1.0)
18
+ logger (1.5.3)
19
+ mini_portile2 (2.8.9)
20
+ nokogiri (1.16.3)
21
+ mini_portile2 (~> 2.8.2)
22
+ racc (~> 1.4)
23
+ parallel (2.3.0)
24
+ parser (3.3.13.0)
25
+ ast (~> 2.4.1)
26
+ racc
27
+ prism (1.9.0)
28
+ racc (1.8.1)
29
+ rainbow (3.1.1)
30
+ rake (13.0.6)
31
+ regexp_parser (2.13.1)
32
+ rspec (3.12.0)
33
+ rspec-core (~> 3.12.0)
34
+ rspec-expectations (~> 3.12.0)
35
+ rspec-mocks (~> 3.12.0)
36
+ rspec-core (3.12.2)
37
+ rspec-support (~> 3.12.0)
38
+ rspec-expectations (3.12.3)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.12.0)
41
+ rspec-mocks (3.12.6)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.12.0)
44
+ rspec-support (3.12.1)
45
+ rubocop (1.92.1)
46
+ json (>= 2.3)
47
+ language_server-protocol (~> 3.17.0.2)
48
+ lint_roller (~> 1.1.0)
49
+ parallel (>= 1.10)
50
+ parser (>= 3.3.0.2)
51
+ rainbow (>= 2.2.2, < 4.0)
52
+ regexp_parser (>= 2.11.3, < 3.0)
53
+ rubocop-ast (>= 1.49.0, < 2.0)
54
+ ruby-progressbar (~> 1.7)
55
+ unicode-display_width (>= 2.4.0, < 4.0)
56
+ rubocop-ast (1.50.0)
57
+ parser (>= 3.3.7.2)
58
+ prism (~> 1.7)
59
+ rubocop-performance (1.27.0)
60
+ lint_roller (~> 1.1)
61
+ rubocop (>= 1.89.0, < 2.0)
62
+ rubocop-ast (>= 1.47.1, < 2.0)
63
+ rubocop-rake (0.7.1)
64
+ lint_roller (~> 1.1)
65
+ rubocop (>= 1.72.1)
66
+ rubocop-rspec (3.10.2)
67
+ lint_roller (~> 1.1)
68
+ regexp_parser (>= 2.0)
69
+ rubocop (~> 1.86, >= 1.86.2)
70
+ ruby-progressbar (1.13.0)
71
+ unicode-display_width (3.3.0)
72
+ unicode-emoji (~> 4.3)
73
+ unicode-emoji (4.3.0)
74
+
75
+ PLATFORMS
76
+ x86_64-darwin-22
77
+ x86_64-darwin-23
78
+
79
+ DEPENDENCIES
80
+ logger (~> 1.5)
81
+ rspec (~> 3.12)
82
+ rubocop (~> 1.92)
83
+ rubocop-performance (~> 1.27)
84
+ rubocop-rake (~> 0.7)
85
+ rubocop-rspec (~> 3.8)
86
+ xsd!
87
+
88
+ BUNDLED WITH
89
+ 2.4.11
data/README.md CHANGED
@@ -61,6 +61,16 @@ element.multiple_allowed? # => true
61
61
  element.required? # => false
62
62
  ```
63
63
 
64
+ ### Imports and the absent namespace
65
+
66
+ XML Schema leaves it to the processor whether every `<xs:import>` of a namespace is read or only the first (a
67
+ `schemaLocation` is a hint); libxml2, and so Nokogiri's validator, reads the first. This reader reads them all, and
68
+ resolves a name within the include tree of the schema the name is written in. That is what a set of schemas with no
69
+ target namespace needs when one schema imports them all: every document a schema of its own, with its own copies of
70
+ common types that may differ from one copy to another. Put together into one schema they break the rule that names
71
+ are unique within a namespace, so a validator assembling one schema of them (`Schema#validate_xml`, which validates
72
+ through Nokogiri) sees only the first of them.
73
+
64
74
  ## Development
65
75
 
66
76
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can
data/Rakefile CHANGED
@@ -15,6 +15,54 @@ RuboCop::RakeTask.new do |t|
15
15
  t.options = ['--display-cop-names']
16
16
  end
17
17
 
18
+ namespace :rbs do
19
+ desc 'Generate sig/xsd_members.rbs: the readers BaseObject.property, .child and .link define for each class'
20
+ task :members do
21
+ $LOAD_PATH.unshift File.expand_path('lib', __dir__)
22
+ require 'xsd'
23
+
24
+ scalars = { string: 'String', boolean: 'bool', integer: 'Integer', array: 'Array[untyped]' }
25
+ class_of = ->(type) { XSD::XML::CLASS_MAP[type.to_s]&.name&.delete_prefix('XSD::') || 'BaseObject' }
26
+ lines = []
27
+ XSD.constants.map { |name| XSD.const_get(name) }.select { |k| k.is_a?(Class) && k <= XSD::BaseObject }
28
+ .sort_by(&:name).each do |klass|
29
+ superclass = klass.superclass
30
+ own = lambda do |kind|
31
+ members = klass.public_send(kind)
32
+ superclass.respond_to?(kind) ? members.reject { |name, spec| superclass.public_send(kind)[name] == spec } : members
33
+ end
34
+ # a method the class or an ancestor defines itself wins over the reader, and is declared in xsd.rbs
35
+ explicit = lambda do |name|
36
+ klass.method_defined?(name) && klass.instance_method(name).owner.name && klass.instance_method(name).owner != Object
37
+ end
38
+ readers = {}
39
+ own.call(:properties).each do |name, spec|
40
+ type = scalars.fetch(spec[:type], 'untyped')
41
+ type = "#{type}?" unless spec[:required] || spec.key?(:default) || type.start_with?('Array')
42
+ type = '(Integer | :unbounded)' if name == :max_occurs
43
+ readers[name] ||= type unless explicit.call(name)
44
+ end
45
+ [*own.call(:children), *own.call(:links)].each do |name, spec|
46
+ type = spec[:type]
47
+ readers[name] ||= (type.is_a?(Array) ? "Array[#{class_of.call(type.first)}]" : "#{class_of.call(type)}?") unless explicit.call(name)
48
+ end
49
+ next if readers.empty?
50
+
51
+ lines << " class #{klass.name.delete_prefix('XSD::')}"
52
+ readers.each { |name, type| lines << " def #{name}: () -> #{type}" }
53
+ lines << ' end' << ''
54
+ end
55
+ File.write(File.expand_path('sig/xsd_members.rbs', __dir__), <<~RBS)
56
+ # Generated by `rake rbs:members` — do not edit. The readers BaseObject's `property`, `child` and `link`
57
+ # define for each class: a property is its value (nil when absent and without a default), a child or a link
58
+ # the object or objects it names.
59
+ module XSD
60
+ #{lines.join("\n").rstrip}
61
+ end
62
+ RBS
63
+ end
64
+ end
65
+
18
66
  desc 'Run tests'
19
67
  task test: :spec
20
68
 
@@ -73,7 +73,7 @@ module XSD
73
73
  # @param [Boolean] deep
74
74
  # @return Nokogiri::XML::NodeSet
75
75
  def nodes(name = :*, deep = false)
76
- node.xpath("./#{deep ? '/' : ''}xs:#{name}", XML_SCHEMA_NS)
76
+ node.xpath("./#{'/' if deep}xs:#{name}", XML_SCHEMA_NS)
77
77
  end
78
78
 
79
79
  # Get schemas by namespace or prefix
@@ -409,7 +409,7 @@ module XSD
409
409
  # @param [String, nil] string
410
410
  # @return String, nil
411
411
  def nil_if_empty(string)
412
- string&.empty? ? nil : string
412
+ string && string.empty? ? nil : string
413
413
  end
414
414
 
415
415
  private
@@ -7,6 +7,7 @@ module XSD
7
7
  # https://www.w3schools.com/xml/el_attribute.asp
8
8
  class Attribute < BaseObject
9
9
  prepend CachedDataType
10
+
10
11
  TYPE_PROPERTY = :type
11
12
 
12
13
  include SimpleTyped
@@ -6,6 +6,7 @@ module XSD
6
6
  # https://www.w3schools.com/xml/el_element.asp
7
7
  class Element < BaseObject
8
8
  prepend CachedDataType
9
+
9
10
  TYPE_PROPERTY = :type
10
11
 
11
12
  include MinMaxOccurs
@@ -15,12 +15,21 @@ module XSD
15
15
  # @return String, nil
16
16
  property :schemaLocation, :string
17
17
 
18
- # Get imported schema
18
+ # Get imported schema: the one at the import's location when that one is loaded already; otherwise one known for
19
+ # the namespace, added manually or by another import. Not for the absent namespace: schemas with no target
20
+ # namespace are each a schema of its own (a provider's documents, every one with its own copies of the common
21
+ # types), so every import of it is read from its own location.
19
22
  # @return Schema
20
23
  def imported_schema
21
- # a schema for the namespace may already be known, either added manually or by another import
22
- known_schema = reader.schemas_for_namespace(namespace).find { |s| !s.equal?(schema) }
23
- return known_schema if known_schema
24
+ if schema_location
25
+ located = reader.loaded_schema(schema_location, base: schema.location)
26
+ return located if located
27
+ end
28
+
29
+ unless namespace.nil?
30
+ known_schema = reader.schemas_for_namespace(namespace).find { |s| !s.equal?(schema) }
31
+ return known_schema if known_schema
32
+ end
24
33
 
25
34
  unless schema_location
26
35
  raise ImportError,
@@ -62,14 +62,14 @@ module XSD
62
62
  def collect_elements(include_base = false)
63
63
  # Don't include elements from base by default, that will lead to element duplicates because
64
64
  # elements in complex restriction MUST be all listen in restricting type
65
- super(include_base)
65
+ super
66
66
  end
67
67
 
68
68
  # Get all available attributes on the current stack level, optionally including base type attributes
69
69
  # @param [Boolean] include_base
70
70
  # @return Array<Attribute>
71
71
  def collect_attributes(include_base = true)
72
- result = super(include_base)
72
+ result = super
73
73
  # Filter restricted attributes to avoid duplicates from restricting and restricted type
74
74
  include_base ? result.to_h { |item| [item.name, item] }.values : result
75
75
  end
@@ -190,7 +190,10 @@ module XSD
190
190
  # @param [String, nil] namespace
191
191
  # @return Boolean
192
192
  def targets_namespace?(namespace)
193
- namespace == target_namespace || namespaces[namespace.nil? ? 'xmlns' : "xmlns:#{namespace}"] == target_namespace ||
193
+ key = namespace.nil? ? 'xmlns' : "xmlns:#{namespace}"
194
+ # a prefix names a namespace only when the schema declares it: looking up a namespace URI as a prefix finds
195
+ # nothing, and nothing must not pass for the absent target namespace of a schema that has none
196
+ namespace == target_namespace || (namespaces.key?(key) && namespaces[key] == target_namespace) ||
194
197
  # an unqualified name in a chameleon schema names something of the namespace it was included into
195
198
  (chameleon? && namespace.nil? && !namespaces.key?('xmlns'))
196
199
  end
@@ -9,7 +9,7 @@ module XSD
9
9
  # @!attribute member_types
10
10
  # @return Array<String>
11
11
  property :memberTypes, :array, default: [] do |union|
12
- union.node['memberTypes']&.split(' ')
12
+ union.node['memberTypes']&.split
13
13
  end
14
14
 
15
15
  # Nested simple and built-in types
data/lib/xsd/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module XSD
4
- VERSION = '2.9.2'
4
+ VERSION = '2.9.4'
5
5
  end
data/lib/xsd/xml.rb CHANGED
@@ -21,7 +21,7 @@ module XSD
21
21
  elsif Pathname.new(location).absolute?
22
22
  File.read(location)
23
23
  else
24
- raise ImportError, "Failed to locate import '#{location}'#{namespace ? " for namespace '#{namespace}'" : ''}"
24
+ raise ImportError, "Failed to locate import '#{location}'#{" for namespace '#{namespace}'" if namespace}"
25
25
  end
26
26
  end
27
27
 
@@ -158,6 +158,14 @@ module XSD
158
158
  @schemas_by_location[expanded] = add_schema_xml(xml, location: expanded)
159
159
  end
160
160
 
161
+ # The schema already read from a location, relative to a base as an import or an include names it
162
+ # @param [String] location
163
+ # @param [String, nil] base Location of the referencing schema
164
+ # @return Schema, nil
165
+ def loaded_schema(location, base: nil)
166
+ @schemas_by_location[expand_location(location.tr('\\', '/'), base)]
167
+ end
168
+
161
169
  # Get first added (considered primary) schema
162
170
  # @return Schema, nil
163
171
  def schema
@@ -256,7 +264,7 @@ module XSD
256
264
  elsif File.file?(location)
257
265
  File.read(location)
258
266
  else
259
- raise ImportError, "Failed to locate import '#{location}'#{namespace ? " for namespace '#{namespace}'" : ''}"
267
+ raise ImportError, "Failed to locate import '#{location}'#{" for namespace '#{namespace}'" if namespace}"
260
268
  end
261
269
  end
262
270