class Xlsxrb::Ooxml::RelationshipsParser
Parses .rels files to build rId -> target, type, and entry mappings.
Public Class Methods
(untyped xml_string) → untyped
Source
# File lib/xlsxrb/ooxml/workbook_parser.rb, line 183 def self.parse(xml_string) parse_all(xml_string)[:targets] end
(String? xml_string) → Hash[Symbol, untyped]
Source
# File lib/xlsxrb/ooxml/workbook_parser.rb, line 160 def self.parse_all(xml_string) return EMPTY_PARSED_RELS if xml_string.nil? || xml_string.empty? sanitized = sanitize_xml_declarations(xml_string) listener = Listener.new begin XmlParser.parse(sanitized, listener) rescue StandardError # Fallback to regex scanning if SAX parser fails on malformed XML listener = Listener.new safe = sanitized.encoding == Encoding::UTF_8 ? sanitized : sanitized.dup.force_encoding(Encoding::UTF_8) safe = safe.scrub unless safe.valid_encoding? safe.scan(%r{<(?:[a-zA-Z0-9_]+:)?Relationship\b([^>]*)/?>}i) do |match| attrs_str = match[0] id = attrs_str[/\bId=["']([^"']+)["']/i, 1] type = attrs_str[/\bType=["']([^"']+)["']/i, 1] target = attrs_str[/\bTarget=["']([^"']+)["']/i, 1] listener.record(id, target, type) end end { entries: listener.entries, targets: listener.relationships, types: listener.types } end
Parses a .rels XML part in one pass, returning targets, types, and entry hashes.
(untyped xml_string) → untyped
Source
# File lib/xlsxrb/ooxml/workbook_parser.rb, line 188 def self.parse_entries(xml_string) parse_all(xml_string)[:entries] end
Parses .rels files to return array of relationship hashes with id, type, and target.