Skip to content

Commit

Permalink
Rename substitution_elements -> substitutable_elements, fix collectio…
Browse files Browse the repository at this point in the history
…n rules as per spec
  • Loading branch information
ekzobrain committed Apr 22, 2024
1 parent 7d06a89 commit d987330
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
28 changes: 21 additions & 7 deletions lib/xsd/objects/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,28 @@ def mixed_content?
complex_type&.mixed_content? || false
end

# Get elements that can appear instead of this one
# Get substitutable elements
# @return Array<Element>
def substitution_elements
# TODO: for now we do not search in parent schemas (that imported current schema)
# TODO: refactor for better namespace handling (use xpath with namespaces or correct comparison)
schema.collect_elements.select do |element|
strip_prefix(element.substitution_group) == name
end
def substitutable_elements
@substitutable_elements ||= begin
if ref
reference.substitutable_elements
elsif !global? || %w[#all substitution].include?(block)
[]
else
# TODO: we search substituted element only in the same schema with head element, is this enough?
# TODO: maybe don't collect all elements but search with xpath first?
schema.collect_elements.select do |element|
strip_prefix(element.substitution_group) == name
end
end
end
end

# Check is this attribute is global (immediate child of schema element)
# @return Boolean
def global?
parent.is_a?(Schema)
end

# Get base data type
Expand Down
12 changes: 12 additions & 0 deletions spec/xsd/element_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,16 @@
end
end
end

context 'with rkzn example file' do
subject(:element) { reader['ChargeCreationRequest', 'ChargeTemplate', 'Discount'] }

let(:file) { fixture_file(%w[rkzn ChargeCreation.xsd], read: false) }

describe '#substitutable_elements' do
it 'gets all substitutable elements' do
expect(element.substitutable_elements.map(&:name)).to eq(%w[DiscountSize DiscountFixed MultiplierSize])
end
end
end
end

0 comments on commit d987330

Please sign in to comment.