Class: XSD::Schema

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

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::NO_ATTRIBUTES_CONTAINER, BaseObject::NO_ELEMENTS_CONTAINER, BaseObject::XML_SCHEMA, BaseObject::XML_SCHEMA_NS

Instance Attribute Summary collapse

Attributes included from ElementContainer

#elements

Attributes included from AttributeContainer

#attribute_groups, #attributes

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, #documentation, #documentation_for, #get_prefix, #initialize, #inspect, link, #map_child, mapped_name, #namespaces, #node, #node_to_object, #nodes, #object_by_name, #parent, property, #reader, #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 ⇒ Object

Global complex types



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 ⇒ Object

Global groups



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

child :groups, [:group]

#imports ⇒ Object

Schema imports



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

child :imports, [:import]

#includes ⇒ Object

Schema includes



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

child :includes, [:include]

#simple_types ⇒ Object

Global simple types



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

child :simple_types, [:simpleType]

#target_namespace ⇒ Object

Optional. A URI reference of the namespace of this schema

Returns:

  • String



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

#collect_attributes ⇒ Object

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



110
111
112
# File 'lib/xsd/objects/schema.rb', line 110

def collect_attributes(*)
  attributes
end

#collect_elements ⇒ Object

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



104
105
106
# File 'lib/xsd/objects/schema.rb', line 104

def collect_elements(*)
  elements
end

#import_by_namespace(ns_or_prefix) ⇒ Object

Get import by namespace or prefix

Parameters:

  • ns_or_prefix (String, nil)

Returns:

  • Import



166
167
168
169
170
171
172
173
# File 'lib/xsd/objects/schema.rb', line 166

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) ⇒ Object

Get children from all loaded schemas

Parameters:

  • name (Symbol)
  • cache (Hash)


152
153
154
155
156
157
158
159
160
161
# File 'lib/xsd/objects/schema.rb', line 152

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.node)

    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



128
129
130
# File 'lib/xsd/objects/schema.rb', line 128

def location
  options[:location]
end

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

Override map_children on schema to get objects from all loaded schemas

Parameters:

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

    Schema nodes already visited, guards against import/include cycles



143
144
145
146
# File 'lib/xsd/objects/schema.rb', line 143

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

#namespace_prefix ⇒ Object

Get schema namespace prefix

Returns:

  • String, nil



122
123
124
# File 'lib/xsd/objects/schema.rb', line 122

def namespace_prefix
  nil_if_empty(@namespace_prefix ||= namespaces.key(XML_SCHEMA)&.sub(/^xmlns:?/, '') || '')
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



116
117
118
# File 'lib/xsd/objects/schema.rb', line 116

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

#targets_namespace?(namespace) ⇒ Boolean

Check if namespace is a target namespace

Parameters:

  • namespace (String, nil)

Returns:

  • (Boolean) —

    Boolean



135
136
137
# File 'lib/xsd/objects/schema.rb', line 135

def targets_namespace?(namespace)
  namespace == target_namespace || namespaces[namespace.nil? ? 'xmlns' : "xmlns:#{namespace}"] == target_namespace
end

#validate ⇒ Object

Validate current schema against XMLSchema 1.0



194
195
196
197
198
199
200
201
202
# File 'lib/xsd/objects/schema.rb', line 194

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)

Raises:



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/xsd/objects/schema.rb', line 177

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