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 www.w3schools.com/xml/el_schema.asp

Constant Summary

Constants inherited from BaseObject

BaseObject::NO_ATTRIBUTES_CONTAINER, BaseObject::NO_ELEMENTS_CONTAINER, BaseObject::XML_SCHEMA

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

#[], #documentation, #documentation_for, #get_prefix, #initialize, #inspect, #map_child, #namespaces, #node, #node_to_object, #nodes, #object_by_name, #parent, #reader, #schemas_for_namespace, #strip_prefix

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_defaultObject

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_defaultObject

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_typesObject

Global complex types



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

child :complex_types, [:complexType]

#element_form_defaultObject

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_defaultObject

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

#groupsObject

Global groups



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

child :groups, [:group]

#importsObject

Schema imports



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

child :imports, [:import]

#includesObject

Schema includes



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

child :includes, [:include]

#simple_typesObject

Global simple types



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

child :simple_types, [:simpleType]

#target_namespaceObject

Optional. A URI reference of the namespace of this schema

Returns:

  • String



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

property :targetNamespace, :string

#versionObject

Optional. Specifies the version of the schema

Returns:

  • String



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

property :version, :string

#xmlnsObject

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_attributesObject

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_elementsObject

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



160
161
162
163
164
165
166
167
# File 'lib/xsd/objects/schema.rb', line 160

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)


143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/xsd/objects/schema.rb', line 143

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

  (includes + imports).map do |import|
    key = import.respond_to?(:namespace) && import.namespace ? import.namespace : import.schema_location
    if cache.key?(key)
      nil
    else
      cache[key] = true
      import.imported_schema.map_children(name, cache)
    end
  end.compact.flatten
end

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

Override map_children on schema to get objects from all loaded schemas

Parameters:

  • name (Symbol)


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

def map_children(name, cache = {})
  super(name) + import_map_children(name, cache)
end

#namespace_prefixObject

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

#schemaObject

Get current schema object

Returns:

  • Schema



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

def schema
  self
end

#target_namespace_prefixObject

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



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

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

#validateObject

Validate current schema against XMLSchema 1.0



186
187
188
189
190
191
192
193
194
# File 'lib/xsd/objects/schema.rb', line 186

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:



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/xsd/objects/schema.rb', line 171

def validate_xml(xml)
  # validate input
  raise ValidationError unless xml.is_a?(Nokogiri::XML::Document) || xml.is_a?(Pathname) || xml.is_a?(String)

  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