Class: XSD::Schema
- Inherits:
-
BaseObject
- Object
- BaseObject
- XSD::Schema
- 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
-
#attribute_form_default ⇒ Object
Optional.
-
#block_default ⇒ Object
Optional.
-
#complex_types ⇒ Array[ComplexType]
Global complex types.
-
#element_form_default ⇒ Object
Optional.
-
#final_default ⇒ Object
Optional.
-
#groups ⇒ Array[Group]
Global groups.
-
#imports ⇒ Array[Import]
Schema imports.
-
#includes ⇒ Array[Include]
Schema includes.
-
#simple_types ⇒ Array[SimpleType]
Global simple types.
-
#target_namespace ⇒ Object
Target namespace of the schema.
-
#version ⇒ Object
Optional.
-
#xmlns ⇒ Object
A URI reference that specifies one or more namespaces for use in this schema.
Attributes inherited from BaseObject
Instance Method Summary collapse
- #attribute_groups ⇒ Array[AttributeGroup]
- #attributes ⇒ Array[Attribute]
-
#chameleon? ⇒ Object
Is this a schema with no target namespace, seen as part of the namespace of a schema including it?.
-
#chameleon_source ⇒ Object
The schema a chameleon view is a view of.
-
#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.
-
#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).
- #elements ⇒ Array[Element]
-
#import_by_namespace(ns_or_prefix) ⇒ Object
Get import by namespace or prefix.
-
#import_map_children(name, cache) ⇒ ::Array[untyped], untyped
Get children from all loaded schemas.
-
#location ⇒ Object
Location the schema was read from, used to resolve relative import/include locations.
-
#map_children(name, cache = {}.compare_by_identity) ⇒ Object
Override map_children on schema to get objects from all loaded schemas.
-
#namespace_prefix ⇒ Object
Get schema namespace prefix.
-
#object_cache ⇒ Object
Objects made of this schema's nodes.
-
#schema ⇒ Object
Get current schema object.
-
#target_namespace_prefix ⇒ Object
Get target namespace prefix.
-
#targets_namespace?(namespace) ⇒ Object
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.
-
#with_includes ⇒ Object
This schema and every schema it includes, those included in turn too.
Methods included from ElementContainer
Methods included from AttributeContainer
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
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 ⇒ Array[ComplexType]
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 ⇒ Array[Group]
Global groups
84 |
# File 'lib/xsd/objects/schema.rb', line 84 child :groups, [:group] |
#imports ⇒ Array[Import]
Schema imports
89 |
# File 'lib/xsd/objects/schema.rb', line 89 child :imports, [:import] |
#includes ⇒ Array[Include]
Schema includes
94 |
# File 'lib/xsd/objects/schema.rb', line 94 child :includes, [:include] |
#simple_types ⇒ Array[SimpleType]
Global simple types
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.
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
#attribute_groups ⇒ Array[AttributeGroup]
188 |
# File 'sig/xsd_members.rbs', line 188
def attribute_groups: () -> Array[AttributeGroup]
|
#attributes ⇒ Array[Attribute]
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?
113 114 115 |
# File 'lib/xsd/objects/schema.rb', line 113 def chameleon? .key?(:chameleon_namespace) end |
#chameleon_source ⇒ Object
The schema a chameleon view is a view of
119 120 121 |
# File 'lib/xsd/objects/schema.rb', line 119 def chameleon_source [:chameleon_source] end |
#collect_attributes ⇒ Object
Get all available attributes on the current stack level, for schema same as attributes
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
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.
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]
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
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
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
185 186 187 |
# File 'lib/xsd/objects/schema.rb', line 185 def location [:location] end |
#map_children(name, cache = {}.compare_by_identity) ⇒ Object
Override map_children on schema to get objects from all loaded schemas
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
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.
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
98 99 100 |
# File 'lib/xsd/objects/schema.rb', line 98 def schema self end |
#target_namespace_prefix ⇒ Object
Get target namespace prefix
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
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
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 = e. + (e.file ? " in file '#{File.basename(e.file)}'" : '') raise ValidationError, end end |
#validate_xml(xml) ⇒ Object
Validate XML against current schema
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
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 |