Skip to content

Commit

Permalink
Fix XML parser with non-root node input
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzobrain committed Apr 9, 2024
1 parent 935b8ec commit fc7af31
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## [Unreleased]

## [2.5.2] - 2024-04-09

- Fix XML parser with non-root node input

## [2.5.1] - 2024-04-08

- First XML parser implementation
Expand Down
4 changes: 3 additions & 1 deletion lib/xsd/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ def build_element(xml, element, data, namespaces = {})
else
has_text = item.is_a?(Hash)
if has_text && element.complex_type&.nodes(:any, true)&.any?
xml.tag!("#{prefix}:#{element.name}", attributes) { |res| res << item['#text'].to_s }
Array.wrap(item['#text']).each do |text|
xml.tag!("#{prefix}:#{element.name}", attributes) { |res| res << text.to_s }
end
else
value = has_text ? item['#text'] : item
xml.tag!("#{prefix}:#{element.name}", attributes, (value == '' ? nil : value))
Expand Down
2 changes: 1 addition & 1 deletion lib/xsd/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module XSD
VERSION = '2.5.1'
VERSION = '2.5.2'
end
13 changes: 9 additions & 4 deletions lib/xsd/xml_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def parse_xml(data)
element = data.is_a?(String) ? Nokogiri::XML(data).root : data

result = {}
@path_offset = element.path.rindex('/')
process_element(element, result)

result
Expand All @@ -21,13 +22,13 @@ def parse_xml(data)
# @param [Hash] result
def process_element(element, result)
name = element.name
path = element.path[1..].split('/').map { |n| n.split(':').last.split('[').first }
logger.debug('XSD::XMLParser') { "Processing element at path #{element.path}" }
path = get_path(element)[1..].split('/').map { |n| n.split(':').last.split('[').first }
logger.debug('XSD::XMLParser') { "Processing element at path #{get_path(element)}" }

# find element in XSD schema
definition = self[*path]
if definition.nil?
logger.debug('XSD::XMLParser') { "Not found XSD definition for element at #{element.path}" }
logger.debug('XSD::XMLParser') { "Not found XSD definition for element at #{get_path(element)}" }
return
end

Expand All @@ -52,7 +53,7 @@ def process_element(element, result)
key = "@#{attribute.name}"
attr_definition = definition[key]
if attr_definition.nil?
logger.debug('XSD::XMLParser') { "Not found XSD definition for attribute at #{attribute.path}" }
logger.debug('XSD::XMLParser') { "Not found XSD definition for attribute at #{get_path(attribute)}" }
next
end

Expand Down Expand Up @@ -102,5 +103,9 @@ def cast_type(value, definition)
value
end
end

def get_path(node)
node.path[@path_offset..]
end
end
end

0 comments on commit fc7af31

Please sign in to comment.