Class: Lutaml::Xsd::Validation::SchemaLocationExtractor

Inherits:
Object
  • Object
show all
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.

Examples:

Extract schema locations

extractor = SchemaLocationExtractor.new(xml_document)
locations = extractor.extract_schema_locations
# => { "http://example.com" => "schema.xsd" }

Extract no-namespace schema location

location = extractor.extract_no_namespace_schema_location
# => "schema.xsd"

Constant Summary collapse

XSI_NAMESPACE =

XML Schema instance namespace

"http://www.w3.org/2001/XMLSchema-instance"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ SchemaLocationExtractor

Initialize a new SchemaLocationExtractor

Parameters:



30
31
32
# File 'lib/lutaml/xsd/validation/schema_location_extractor.rb', line 30

def initialize(document)
  @document = document
end

Instance Attribute Details

#documentXmlDocument (readonly)

Returns The XML document to extract from.

Returns:



25
26
27
# File 'lib/lutaml/xsd/validation/schema_location_extractor.rb', line 25

def document
  @document
end

Instance Method Details

#all_schema_urisArray<String>

Get all schema location URIs (for downloading/resolving)

Returns:

  • (Array<String>)

    List of all schema location URIs



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_allHash

Extract all schema locations (both namespaced and non-namespaced)

Examples:

all_locations = extractor.extract_all
# => {
#   namespaced: { "http://example.com" => "schema.xsd" },
#   no_namespace: "schema.xsd"
# }

Returns:

  • (Hash)

    Hash with :namespaced and :no_namespace keys



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_locationString?

Extract no-namespace schema location

The xsi:noNamespaceSchemaLocation attribute contains a single schema location for elements without a namespace.

Examples:

location = extractor.extract_no_namespace_schema_location
# => "schema.xsd"

Returns:

  • (String, nil)

    Schema location or nil if not present



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_locationsHash<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”

Examples:

locations = extractor.extract_schema_locations
# => {
#   "http://example.com" => "schema.xsd",
#   "http://other.com" => "other.xsd"
# }

Returns:

  • (Hash<String, String>)

    Map of namespace URI to schema location



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

Returns:

  • (Boolean)


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_hHash

Convert to hash representation

Returns:

  • (Hash)


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