Skip to content

Commit

Permalink
Add element lookup with namespaces
Browse files Browse the repository at this point in the history
Fix XML parser on element with duplicated names and different namespaces
  • Loading branch information
ekzobrain committed Apr 10, 2024
1 parent 04c0e8b commit faae40f
Show file tree
Hide file tree
Showing 11 changed files with 21,596 additions and 4 deletions.
23 changes: 21 additions & 2 deletions lib/xsd/base_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ def [](*args)
curname = curname.to_s

if curname[0] == '@'
result = result.collect_attributes.find { |attr| attr.name == curname[1..-1] }
result = result.collect_attributes.find { |attr| definition_match?(attr, curname[1..]) }
else
result = result.collect_elements.find { |elem| curname == '*' || elem.name == curname }
result = result.collect_elements.find { |elem| definition_match?(elem, curname) }
end
end

Expand Down Expand Up @@ -366,5 +366,24 @@ def self.mapped_name
def nil_if_empty(string)
string&.empty? ? nil : string
end

private

def definition_match?(definition, query)
actual_definition = definition.referenced? ? definition.reference : definition

if query.start_with?('{')
parts = query[1..].split('}')
raise Error, "Invalid element/attribute query: #{query}" if parts.size != 2

namespace, name = parts

return false if namespace != actual_definition.target_namespace
else
name = query
end

name == '*' || actual_definition.name == name
end
end
end
4 changes: 2 additions & 2 deletions lib/xsd/xml_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def parse_xml(data)
# @param [Array<String>] path
def process_element(element, result, path)
name = element.name
path = [*path, name]
path = [*path, "{#{element.namespace.href}}#{name}"]
logger.debug('XSD::XMLParser') { "Processing element <#{name}> at path #{element.path}" }

# find element in XSD schema
Expand All @@ -34,7 +34,7 @@ def process_element(element, result, path)
else
result['#text'] = element.to_xml
end
logger.debug('XSD::XMLParser') { "Element definition not found, return as <any> #text at #{element.path}" }
logger.info('XSD::XMLParser') { "Element definition not found, return as <any> #text at #{element.path}" }
return
end

Expand Down
11,997 changes: 11,997 additions & 0 deletions spec/fixtures/rupm/Common_V4_PM.xsd

Large diffs are not rendered by default.

655 changes: 655 additions & 0 deletions spec/fixtures/rupm/RUCommon_V4_PM.xsd

Large diffs are not rendered by default.

1,543 changes: 1,543 additions & 0 deletions spec/fixtures/rupm/RUPatentPublication_V4_Lgc.xsd

Large diffs are not rendered by default.

Loading

0 comments on commit faae40f

Please sign in to comment.