Class: XSD::Schema

Inherits:
BaseObject show all
Includes:
AttributeContainer, ElementContainer
Defined in:
lib/xsd/objects/schema.rb,
sig/xsd.rbs,
sig/xsd_members.rbs

Overview

The schema element defines the root element of a schema. Parent elements: NONE https://www.w3schools.com/xml/el_schema.asp

Constant Summary

Constants inherited from BaseObject

BaseObject::NOT_A_MEMBER, BaseObject::NO_ATTRIBUTES_CONTAINER, BaseObject::NO_ELEMENTS_CONTAINER, BaseObject::XML_SCHEMA, BaseObject::XML_SCHEMA_NS

Instance Attribute Summary collapse

Attributes inherited from BaseObject

#id, #options

Instance Method Summary collapse

Methods included from ElementContainer

included

Methods included from AttributeContainer

included

Methods inherited from BaseObject

#[], child, define_member_reader, #documentation, #documentation_for, #get_prefix, #initialize, #inspect, link, #map_child, mapped_name, member_readers, #method_missing, #namespaces, #nil_if_empty, #node, #node_to_object, #nodes, #object_by_name, #parent, property, #read_member, #reader, #respond_to_missing?, #schemas_for_namespace, #strip_prefix, to_underscore

Constructor Details

This class inherits a constructor from XSD::BaseObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class XSD::BaseObject

Instance Attribute Details

#attribute_form_default ⇒ Object

Optional. The form for attributes declared in the target namespace of this schema. The value must be "qualified" or "unqualified". Default is "unqualified". "unqualified" indicates that attributes from the target namespace are not required to be qualified with the namespace prefix. "qualified" indicates that attributes from the target namespace must be qualified with the namespace prefix

Returns:

  • String



20
# File 'lib/xsd/objects/schema.rb', line 20

property :attributeFormDefault, :string, default: 'unqualified'

#block_default ⇒ Object

Optional. Specifies the default value of the block attribute on element and complexType elements in the target namespace. The block attribute prevents a complex type (or element) that has a specified type of derivation from being used in place of this complex type. This value can contain #all or a list that is a subset of extension, restriction, or substitution:

extension - prevents complex types derived by extension
restriction - prevents complex types derived by restriction
substitution - prevents substitution of elements
#all - prevents all derived complex types

Returns:

  • String



40
# File 'lib/xsd/objects/schema.rb', line 40

property :blockDefault, :string

#complex_types ⇒ Array[ComplexType]

Global complex types

Returns:



74
# File 'lib/xsd/objects/schema.rb', line 74

child :complex_types, [:complexType]

#element_form_default ⇒ Object

Optional. The form for elements declared in the target namespace of this schema. The value must be "qualified" or "unqualified". Default is "unqualified". "unqualified" indicates that elements from the target namespace are not required to be qualified with the namespace prefix. "qualified" indicates that elements from the target namespace must be qualified with the namespace prefix

Returns:

  • String



28
# File 'lib/xsd/objects/schema.rb', line 28

property :elementFormDefault, :string, default: 'unqualified'

#final_default ⇒ Object

Optional. Specifies the default value of the final attribute on element, simpleType, and complexType elements in the target namespace. The final attribute prevents a specified type of derivation of an element, simpleType, or complexType element. For element and complexType elements, this value can contain #all or a list that is a subset of extension or restriction. For simpleType elements, this value can additionally contain list and union:

extension - prevents derivation by extension
restriction - prevents derivation by restriction
list - prevents derivation by list
union - prevents derivation by union
#all - prevents all derivation

Returns:

  • String



53
# File 'lib/xsd/objects/schema.rb', line 53

property :finalDefault, :string

#groups ⇒ Array[Group]

Global groups

Returns:



84
# File 'lib/xsd/objects/schema.rb', line 84

child :groups, [:group]

#imports ⇒ Array[Import]

Schema imports

Returns:



89
# File 'lib/xsd/objects/schema.rb', line 89

child :imports, [:import]

#includes ⇒ Array[Include]

Schema includes

Returns:



94
# File 'lib/xsd/objects/schema.rb', line 94

child :includes, [:include]

#simple_types ⇒ Array[SimpleType]

Global simple types

Returns:



79
# File 'lib/xsd/objects/schema.rb', line 79

child :simple_types, [:simpleType]

#target_namespace ⇒ Object

Target namespace of the schema. For a chameleon view (see XML#chameleon), the namespace of the schema that includes it — the one everything it declares belongs to.

Returns:

  • String, nil



58
# File 'lib/xsd/objects/schema.rb', line 58

property :targetNamespace, :string

#version ⇒ Object

Optional. Specifies the version of the schema

Returns:

  • String



63
# File 'lib/xsd/objects/schema.rb', line 63

property :version, :string

#xmlns ⇒ Object

A URI reference that specifies one or more namespaces for use in this schema. If no prefix is assigned, the schema components of the namespace can be used with unqualified references

Returns:

  • String



69
# File 'lib/xsd/objects/schema.rb', line 69

property :xmlns, :string

Instance Method Details

#attribute_groups ⇒ Array[AttributeGroup]

Returns:



188
# File 'sig/xsd_members.rbs', line 188

def attribute_groups: () -> Array[AttributeGroup]

#attributes ⇒ Array[Attribute]

Returns:



187
# File 'sig/xsd_members.rbs', line 187

def attributes: () -> Array[Attribute]

#chameleon? ⇒ Object

Is this a schema with no target namespace, seen as part of the namespace of a schema including it?

Returns:

  • Boolean



113
114
115
# File 'lib/xsd/objects/schema.rb', line 113

def chameleon?
  options.key?(:chameleon_namespace)
end

#chameleon_source ⇒ Object

The schema a chameleon view is a view of

Returns:

  • Schema, nil



119
120
121
# File 'lib/xsd/objects/schema.rb', line 119

def chameleon_source
  options[:chameleon_source]
end

#collect_attributes ⇒ Object

Get all available attributes on the current stack level, for schema same as attributes

Parameters:

  • (Object)

Returns:

  • (Object)


167
168
169
# File 'lib/xsd/objects/schema.rb', line 167

def collect_attributes(*)
  attributes
end

#collect_elements ⇒ Object

Get all available elements on the current stack level, for schema same as elements

Parameters:

  • (Object)

Returns:

  • (Object)


161
162
163
# File 'lib/xsd/objects/schema.rb', line 161

def collect_elements(*)
  elements
end

#declaration_node(node_name, name) ⇒ Object

The node of a top-level declaration of this schema, by its kind and name — what a reference by name looks up (BaseObject#object_by_name). The children are indexed in one pass on the first lookup: a query per lookup scans all of them again, and a large schema is looked up in once for every reference to it.

Parameters:

  • node_name (Symbol, String) —

    kind of declaration: complexType, simpleType, element, group, ...

  • name (String) —

    local name

Returns:

  • Nokogiri::XML::Element, nil



136
137
138
139
140
141
142
143
144
# File 'lib/xsd/objects/schema.rb', line 136

def declaration_node(node_name, name)
  @declaration_nodes ||= node.element_children.each_with_object({}) do |child, index|
    next unless child.namespace&.href == XML_SCHEMA

    # the first one, as a query would find it
    index[[child.name, child['name']]] ||= child
  end
  @declaration_nodes[[node_name.to_s, name]]
end

#elements ⇒ Array[Element]

Returns:



189
# File 'sig/xsd_members.rbs', line 189

def elements: () -> Array[Element]

#import_by_namespace(ns_or_prefix) ⇒ Object

Get import by namespace or prefix

Parameters:

  • ns_or_prefix (String, nil)

Returns:

  • Import



228
229
230
231
232
233
234
235
# File 'lib/xsd/objects/schema.rb', line 228

def import_by_namespace(ns_or_prefix)
  aliases = [
    ns_or_prefix,
    namespaces["xmlns:#{(ns_or_prefix || '').gsub(/^xmlns:/, '')}"]
  ].compact

  imports.find { |import| aliases.include?(import.namespace) }
end

#import_map_children(name, cache) ⇒ ::Array[untyped], untyped

Get children from all loaded schemas

Parameters:

  • name (Symbol)
  • cache (Hash)

Returns:

  • (::Array[untyped], untyped)


214
215
216
217
218
219
220
221
222
223
# File 'lib/xsd/objects/schema.rb', line 214

def import_map_children(name, cache)
  return [] if %i[import include].include?(name.to_sym)

  (includes + imports).flat_map do |import|
    imported = import.imported_schema
    next [] if cache.key?(imported)

    imported.map_children(name, cache)
  end
end

#location ⇒ Object

Location the schema was read from, used to resolve relative import/include locations

Returns:

  • String, nil



185
186
187
# File 'lib/xsd/objects/schema.rb', line 185

def location
  options[:location]
end

#map_children(name, cache = {}.compare_by_identity) ⇒ Object

Override map_children on schema to get objects from all loaded schemas

Parameters:

  • name (Symbol)
  • cache (Hash) (defaults to: {}.compare_by_identity) —

    Schema nodes already visited, guards against import/include cycles

Returns:

  • (Object)


205
206
207
208
# File 'lib/xsd/objects/schema.rb', line 205

def map_children(name, cache = {}.compare_by_identity)
  cache[self] = true
  super(name) + import_map_children(name, cache)
end

#namespace_prefix ⇒ Object

Get schema namespace prefix

Returns:

  • String, nil



179
180
181
# File 'lib/xsd/objects/schema.rb', line 179

def namespace_prefix
  nil_if_empty(@namespace_prefix ||= namespaces.key(XML_SCHEMA)&.sub(/^xmlns:?/, '') || '')
end

#object_cache ⇒ Object

Objects made of this schema's nodes. A chameleon view keeps its own: the same node is a different definition in each namespace that includes it.

Returns:

  • Hash



126
127
128
# File 'lib/xsd/objects/schema.rb', line 126

def object_cache
  chameleon? ? (@object_cache ||= {}.compare_by_identity) : reader.object_cache
end

#schema ⇒ Object

Get current schema object

Returns:

  • Schema



98
99
100
# File 'lib/xsd/objects/schema.rb', line 98

def schema
  self
end

#target_namespace_prefix ⇒ Object

Get target namespace prefix

Returns:

  • String, nil



173
174
175
# File 'lib/xsd/objects/schema.rb', line 173

def target_namespace_prefix
  nil_if_empty(@target_namespace_prefix ||= namespaces.key(target_namespace)&.sub(/^xmlns:?/, '') || '')
end

#targets_namespace?(namespace) ⇒ Object

Check if namespace is a target namespace

Parameters:

  • namespace (String, nil)

Returns:

  • Boolean



192
193
194
195
196
197
198
199
# File 'lib/xsd/objects/schema.rb', line 192

def targets_namespace?(namespace)
  key = namespace.nil? ? 'xmlns' : "xmlns:#{namespace}"
  # a prefix names a namespace only when the schema declares it: looking up a namespace URI as a prefix finds
  # nothing, and nothing must not pass for the absent target namespace of a schema that has none
  namespace == target_namespace || (namespaces.key?(key) && namespaces[key] == target_namespace) ||
    # an unqualified name in a chameleon schema names something of the namespace it was included into
    (chameleon? && namespace.nil? && !namespaces.key?('xmlns'))
end

#validate ⇒ Object

Validate current schema against XMLSchema 1.0

Returns:

  • (Object)


256
257
258
259
260
261
262
263
264
# File 'lib/xsd/objects/schema.rb', line 256

def validate
  begin
    schema_validator
  rescue Nokogiri::XML::SyntaxError => e
    # TODO: display import map name for imported_xsd
    message = e.message + (e.file ? " in file '#{File.basename(e.file)}'" : '')
    raise ValidationError, message
  end
end

#validate_xml(xml) ⇒ Object

Validate XML against current schema

Parameters:

  • xml (String, Pathname, Nokogiri::XML::Document)

Returns:

  • (Object)


239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/xsd/objects/schema.rb', line 239

def validate_xml(xml)
  # validate input
  unless xml.is_a?(Nokogiri::XML::Document) || xml.is_a?(Pathname) || xml.is_a?(String)
    raise ValidationError, "XML must be a String, Pathname or Nokogiri::XML::Document, got #{xml.class}"
  end

  begin
    document = xml.is_a?(Nokogiri::XML::Document) ? xml : Nokogiri::XML(xml)
  rescue Nokogiri::XML::SyntaxError => e
    raise ValidationError, e
  end

  errors = schema_validator.validate(document)
  raise ValidationError, errors.map(&:message).join('; ') if errors.any?
end

#with_includes ⇒ Object

This schema and every schema it includes, those included in turn too

Returns:

  • (Object)


148
149
150
151
152
153
154
155
156
157
# File 'lib/xsd/objects/schema.rb', line 148

def with_includes
  found = [self]
  found.each do |schema|
    schema.includes.each do |include|
      included = include.imported_schema
      found << included unless found.any? { |known| known.equal?(included) }
    end
  end
  found
end