Class: XSD::Element

Inherits:
BaseObject show all
Includes:
ComplexTyped, MinMaxOccurs, Named, Referenced, SimpleTyped
Defined in:
lib/xsd/objects/element.rb

Overview

The element element defines an element. Parent elements: schema, choice, all, sequence, group www.w3schools.com/xml/el_element.asp

Constant Summary collapse

TYPE_PROPERTY =
:type

Constants inherited from BaseObject

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

Instance Attribute Summary collapse

Attributes included from Referenced

#ref, #reference

Attributes included from ComplexTyped

#complex_type

Attributes included from SimpleTyped

#simple_type

Attributes included from MinMaxOccurs

#max_occurs, #min_occurs

Attributes inherited from BaseObject

#id, #options

Instance Method Summary collapse

Methods included from Named

#absolute_name, #namespace

Methods included from Referenced

included, #referenced?

Methods included from ComplexTyped

#collect_attributes, #collect_elements, included

Methods included from SimpleTyped

included

Methods included from MinMaxOccurs

#computed_max_occurs, #computed_min_occurs, included, #multiple?, #multiple_allowed?

Methods inherited from BaseObject

#[], #collect_attributes, #collect_elements, #documentation, #documentation_for, #get_prefix, #initialize, #inspect, #map_child, #map_children, #namespaces, #node, #node_to_object, #nodes, #object_by_name, #parent, #reader, #schema, #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

#abstractObject

Optional. Specifies whether the element can be used in an instance document. True indicates that the element cannot appear in the instance document. Instead, another element whose substitutionGroup attribute contains the qualified name (QName) of this element must appear in this element’s place. Default is false

Returns:

  • Boolean



64
# File 'lib/xsd/objects/element.rb', line 64

property :abstract, :boolean, default: false

#blockObject

Optional. Prevents an element with a specified type of derivation from being used in place of this element. This value can contain #all or a list that is a subset of extension, restriction, or equivClass:

extension    - prevents elements derived by extension
restriction  - prevents elements derived by restriction
substitution - prevents elements derived by substitution
#all         - prevents all derived elements

Returns:

  • String, nil



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

property :block, :string

#defaultObject

Optional. Specifies a default value for the element (can only be used if the element’s content is a simple type or text only)

Returns:

  • String, nil



36
# File 'lib/xsd/objects/element.rb', line 36

property :default, :string

#finalObject

Optional. Sets the default value of the final attribute on the element element. This attribute cannot be used if the parent element is not the schema element. This value can contain #all or a list that is a subset of extension or restriction:

extension   - prevents elements derived by extension
restriction - prevents elements derived by restriction
#all        - prevents all derived elements

Returns:

  • String, nil



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

property :final, :string

#fixedObject

Optional. Specifies a fixed value for the element (can only be used if the element’s content is a simple type or text only)

Returns:

  • String, nil



42
# File 'lib/xsd/objects/element.rb', line 42

property :fixed, :string

#formObject

Optional. Specifies the form for the element. “unqualified” indicates that this element is not required to be qualified with the namespace prefix. “qualified” indicates that this element must be qualified with the namespace prefix. The default value is the value of the elementFormDefault attribute of the schema element. This attribute cannot be used if the parent element is the schema element

Returns:

  • String



50
# File 'lib/xsd/objects/element.rb', line 50

property :form, :string, default: proc { schema.element_form_default }

#nameObject

Optional. Specifies a name for the element. This attribute is required if the parent element is the schema element

Returns:

  • String, nil



19
# File 'lib/xsd/objects/element.rb', line 19

property :name, :string

#nillableObject

Optional. Specifies whether an explicit null value can be assigned to the element. True enables an instance of the element to have the null attribute set to true. The null attribute is defined as part of the XML Schema namespace for instances. Default is false

Returns:

  • Boolean



57
# File 'lib/xsd/objects/element.rb', line 57

property :nillable, :boolean, default: false

#substitution_groupObject

Optional. Specifies the name of an element that can be substituted with this element. This attribute cannot be used if the parent element is not the schema element

Returns:

  • String, nil



30
# File 'lib/xsd/objects/element.rb', line 30

property :substitutionGroup, :string

#typeObject

Optional. Specifies either the name of a built-in data type, or the name of a simpleType or complexType element

Returns:

  • String, nil



24
# File 'lib/xsd/objects/element.rb', line 24

property :type, :string

#uniqueObject

Nested unique objects



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

child :unique, [:unique]

Instance Method Details

#attributes?Boolean

Determine if element has attributes

Returns:

  • (Boolean)

    Boolean



123
124
125
# File 'lib/xsd/objects/element.rb', line 123

def attributes?
  complex_type && collect_attributes.any?
end

#complex_content?Boolean

Determine if element has complex content

Returns:

  • (Boolean)

    Boolean



105
106
107
108
# File 'lib/xsd/objects/element.rb', line 105

def complex_content?
  # this is not an opposite to simple_content?, element may have neither
  complex_type&.complex_content? || false
end

#data_typeObject

Get base data type

Returns:

  • String, nil



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

def data_type
  if complex_type
    complex_type.data_type
  elsif simple_type
    simple_type.data_type
  else
    strip_prefix(type)
  end
end

#global?Boolean

Check is this attribute is global (immediate child of schema element)

Returns:

  • (Boolean)

    Boolean



153
154
155
# File 'lib/xsd/objects/element.rb', line 153

def global?
  parent.is_a?(Schema)
end

#mixed_content?Boolean

Determine if element has mixed content

Returns:

  • (Boolean)

    Boolean



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

def mixed_content?
  complex_type&.mixed_content? || false
end

#optional?Boolean

Determine if element is optional

Returns:

  • (Boolean)

    Boolean



99
100
101
# File 'lib/xsd/objects/element.rb', line 99

def optional?
  !required?
end

#required?Boolean

Determine if element is required

Returns:

  • (Boolean)

    Boolean



93
94
95
# File 'lib/xsd/objects/element.rb', line 93

def required?
  computed_min_occurs > 0
end

#simple_content?Boolean

Determine if element has simple content

Returns:

  • (Boolean)

    Boolean



112
113
114
115
116
117
118
119
# File 'lib/xsd/objects/element.rb', line 112

def simple_content?
  # this is not an opposite to complex_content?, element may have neither
  if complex_type
    complex_type.simple_content?
  else
    true
  end
end

#substitutable_elementsObject

Get substitutable elements



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/xsd/objects/element.rb', line 135

def substitutable_elements
  @substitutable_elements ||= begin
                                if ref
                                  reference.substitutable_elements
                                elsif !global? || %w[#all substitution].include?(block)
                                  []
                                else
                                  # TODO: we search substituted element only in the same schema with head element, is this enough?
                                  # TODO: maybe don't collect all elements but search with xpath first?
                                  schema.collect_elements.select do |element|
                                    strip_prefix(element.substitution_group) == name
                                  end
                                end
                              end
end