Class: XSD::Schema
- Inherits:
-
BaseObject
- Object
- BaseObject
- XSD::Schema
- 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
-
#attribute_form_default ⇒ Object
Optional.
-
#block_default ⇒ Object
Optional.
-
#complex_types ⇒ Object
Global complex types.
-
#element_form_default ⇒ Object
Optional.
-
#final_default ⇒ Object
Optional.
-
#groups ⇒ Object
Global groups.
-
#imports ⇒ Object
Schema imports.
-
#includes ⇒ Object
Schema includes.
-
#simple_types ⇒ Object
Global simple types.
-
#target_namespace ⇒ Object
Optional.
-
#version ⇒ Object
Optional.
-
#xmlns ⇒ Object
A URI reference that specifies one or more namespaces for use in this schema.
Attributes included from ElementContainer
Attributes included from AttributeContainer
#attribute_groups, #attributes
Attributes inherited from BaseObject
Instance Method Summary collapse
-
#collect_attributes ⇒ Object
Get all available attributes on the current stack level, for schema same as attributes.
-
#collect_elements ⇒ Object
Get all available elements on the current stack level, for schema same as elements.
-
#import_by_namespace(ns_or_prefix) ⇒ Object
Get import by namespace or prefix.
-
#import_map_children(name, cache) ⇒ Object
Get children from all loaded schemas.
-
#map_children(name, cache = {}) ⇒ Object
Override map_children on schema to get objects from all loaded schemas.
-
#namespace_prefix ⇒ Object
Get schema namespace prefix.
-
#schema ⇒ Object
Get current schema object.
-
#target_namespace_prefix ⇒ Object
Get target namespace prefix.
-
#targets_namespace?(namespace) ⇒ Boolean
Check if namespace is a target namespace.
-
#validate ⇒ Object
Validate current schema against XMLSchema 1.0.
-
#validate_xml(xml) ⇒ Object
Validate XML against current schema.
Methods included from ElementContainer
Methods included from AttributeContainer
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_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
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
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
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
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
58 |
# File 'lib/xsd/objects/schema.rb', line 58 property :targetNamespace, :string |
#version ⇒ Object
Optional. Specifies the version of the schema
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
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
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
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
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_prefix ⇒ Object
Get schema namespace prefix
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
98 99 100 |
# File 'lib/xsd/objects/schema.rb', line 98 def schema self end |
#target_namespace_prefix ⇒ Object
Get target namespace prefix
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
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 |
#validate ⇒ Object
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 = e. + (e.file ? " in file '#{File.basename(e.file)}'" : '') raise ValidationError, end end |
#validate_xml(xml) ⇒ Object
Validate XML against current schema
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 |