Class: Lutaml::Xsd::Validation::SchemaLocationExtractor
- Inherits:
-
Object
- Object
- Lutaml::Xsd::Validation::SchemaLocationExtractor
- Defined in:
- lib/lutaml/xsd/validation/schema_location_extractor.rb
Overview
SchemaLocationExtractor extracts schema locations from XML documents
This class extracts xsi:schemaLocation and xsi:noNamespaceSchemaLocation attributes from XML documents to resolve which XSD schemas should be used for validation.
Constant Summary collapse
- XSI_NAMESPACE =
XML Schema instance namespace
"http://www.w3.org/2001/XMLSchema-instance"
Instance Attribute Summary collapse
-
#document ⇒ XmlDocument
readonly
The XML document to extract from.
Instance Method Summary collapse
-
#all_schema_uris ⇒ Array<String>
Get all schema location URIs (for downloading/resolving).
-
#extract_all ⇒ Hash
Extract all schema locations (both namespaced and non-namespaced).
-
#extract_no_namespace_schema_location ⇒ String?
Extract no-namespace schema location.
-
#extract_schema_locations ⇒ Hash<String, String>
Extract schema locations from xsi:schemaLocation attribute.
-
#has_schema_locations? ⇒ Boolean
Check if document has schema location hints.
-
#initialize(document) ⇒ SchemaLocationExtractor
constructor
Initialize a new SchemaLocationExtractor.
-
#to_h ⇒ Hash
Convert to hash representation.
Constructor Details
#initialize(document) ⇒ SchemaLocationExtractor
Initialize a new SchemaLocationExtractor
30 31 32 |
# File 'lib/lutaml/xsd/validation/schema_location_extractor.rb', line 30 def initialize(document) @document = document end |
Instance Attribute Details
#document ⇒ XmlDocument (readonly)
Returns The XML document to extract from.
25 26 27 |
# File 'lib/lutaml/xsd/validation/schema_location_extractor.rb', line 25 def document @document end |
Instance Method Details
#all_schema_uris ⇒ Array<String>
Get all schema location URIs (for downloading/resolving)
111 112 113 114 115 116 |
# File 'lib/lutaml/xsd/validation/schema_location_extractor.rb', line 111 def all_schema_uris uris = extract_schema_locations.values no_ns = extract_no_namespace_schema_location uris << no_ns if no_ns uris end |
#extract_all ⇒ Hash
Extract all schema locations (both namespaced and non-namespaced)
93 94 95 96 97 98 |
# File 'lib/lutaml/xsd/validation/schema_location_extractor.rb', line 93 def extract_all { namespaced: extract_schema_locations, no_namespace: extract_no_namespace_schema_location, }.compact end |
#extract_no_namespace_schema_location ⇒ String?
Extract no-namespace schema location
The xsi:noNamespaceSchemaLocation attribute contains a single schema location for elements without a namespace.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/lutaml/xsd/validation/schema_location_extractor.rb', line 71 def extract_no_namespace_schema_location root = @document.root_element return nil unless root no_ns_attr = root.attribute( "noNamespaceSchemaLocation", namespace: XSI_NAMESPACE, ) no_ns_attr&.value end |
#extract_schema_locations ⇒ Hash<String, String>
Extract schema locations from xsi:schemaLocation attribute
The xsi:schemaLocation attribute contains pairs of namespace URIs and schema locations: “namespace1 location1 namespace2 location2”
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/lutaml/xsd/validation/schema_location_extractor.rb', line 47 def extract_schema_locations root = @document.root_element return {} unless root schema_location_attr = root.attribute( "schemaLocation", namespace: XSI_NAMESPACE, ) return {} unless schema_location_attr parse_schema_location_pairs(schema_location_attr.value) end |
#has_schema_locations? ⇒ Boolean
Check if document has schema location hints
103 104 105 106 |
# File 'lib/lutaml/xsd/validation/schema_location_extractor.rb', line 103 def has_schema_locations? !extract_schema_locations.empty? || !extract_no_namespace_schema_location.nil? end |
#to_h ⇒ Hash
Convert to hash representation
121 122 123 124 125 126 127 |
# File 'lib/lutaml/xsd/validation/schema_location_extractor.rb', line 121 def to_h { schema_locations: extract_schema_locations, no_namespace_location: extract_no_namespace_schema_location, has_locations: has_schema_locations?, }.compact end |