Class: Lutaml::Xsd::Validation::XmlDocument
- Inherits:
-
Object
- Object
- Lutaml::Xsd::Validation::XmlDocument
- Defined in:
- lib/lutaml/xsd/validation/xml_document.rb
Overview
XmlDocument represents a parsed XML document
This class wraps the parsed Moxml document and provides access to the root element and document-level operations.
Instance Attribute Summary collapse
-
#moxml_document ⇒ Object
readonly
Returns the value of attribute moxml_document.
-
#navigator ⇒ Object
readonly
Returns the value of attribute navigator.
Instance Method Summary collapse
-
#all_elements ⇒ Array<XmlElement>
Get all elements in document order.
-
#elements_by_qualified_name(qualified_name) ⇒ Array<XmlElement>
Find elements by qualified name.
-
#encoding ⇒ String?
Get the XML encoding.
-
#initialize(moxml_document, navigator) ⇒ XmlDocument
constructor
Initialize a new XmlDocument.
-
#inspect ⇒ String
Detailed string representation.
-
#namespace_declarations ⇒ Hash<String, String>
Get all namespace declarations in the document.
-
#root_element ⇒ XmlElement?
Get the root element of the document.
-
#to_h ⇒ Hash
Convert to hash representation.
-
#to_s ⇒ String
String representation.
-
#valid_xml? ⇒ Boolean
Check if document is valid XML.
-
#version ⇒ String?
Get the XML version.
-
#xpath(xpath) ⇒ Array<XmlElement>
Find elements by XPath.
Constructor Details
#initialize(moxml_document, navigator) ⇒ XmlDocument
Initialize a new XmlDocument
25 26 27 28 |
# File 'lib/lutaml/xsd/validation/xml_document.rb', line 25 def initialize(moxml_document, navigator) @moxml_document = moxml_document @navigator = navigator end |
Instance Attribute Details
#moxml_document ⇒ Object (readonly)
Returns the value of attribute moxml_document.
19 20 21 |
# File 'lib/lutaml/xsd/validation/xml_document.rb', line 19 def moxml_document @moxml_document end |
#navigator ⇒ Object (readonly)
Returns the value of attribute navigator.
19 20 21 |
# File 'lib/lutaml/xsd/validation/xml_document.rb', line 19 def navigator @navigator end |
Instance Method Details
#all_elements ⇒ Array<XmlElement>
Get all elements in document order
85 86 87 88 89 |
# File 'lib/lutaml/xsd/validation/xml_document.rb', line 85 def all_elements return [] unless root_element collect_elements(root_element) end |
#elements_by_qualified_name(qualified_name) ⇒ Array<XmlElement>
Find elements by qualified name
95 96 97 |
# File 'lib/lutaml/xsd/validation/xml_document.rb', line 95 def elements_by_qualified_name(qualified_name) all_elements.select { |el| el.qualified_name == qualified_name } end |
#encoding ⇒ String?
Get the XML encoding
71 72 73 |
# File 'lib/lutaml/xsd/validation/xml_document.rb', line 71 def encoding @moxml_document.encoding if @moxml_document.respond_to?(:encoding) end |
#inspect ⇒ String
Detailed string representation
121 122 123 124 |
# File 'lib/lutaml/xsd/validation/xml_document.rb', line 121 def inspect "#<#{self.class.name} " \ "root=#{root_element&.qualified_name.inspect}>" end |
#namespace_declarations ⇒ Hash<String, String>
Get all namespace declarations in the document
42 43 44 45 46 |
# File 'lib/lutaml/xsd/validation/xml_document.rb', line 42 def namespace_declarations return {} unless root_element collect_namespaces(root_element) end |
#root_element ⇒ XmlElement?
Get the root element of the document
33 34 35 36 37 |
# File 'lib/lutaml/xsd/validation/xml_document.rb', line 33 def root_element return nil unless @moxml_document.root XmlElement.new(@moxml_document.root, @navigator) end |
#to_h ⇒ Hash
Convert to hash representation
102 103 104 105 106 107 108 109 |
# File 'lib/lutaml/xsd/validation/xml_document.rb', line 102 def to_h { version: version, encoding: encoding, root_element: root_element&.qualified_name, namespaces: namespace_declarations, }.compact end |
#to_s ⇒ String
String representation
114 115 116 |
# File 'lib/lutaml/xsd/validation/xml_document.rb', line 114 def to_s "XMLDocument(root: #{root_element&.qualified_name})" end |
#valid_xml? ⇒ Boolean
Check if document is valid XML
78 79 80 |
# File 'lib/lutaml/xsd/validation/xml_document.rb', line 78 def valid_xml? !@moxml_document.nil? && !root_element.nil? end |
#version ⇒ String?
Get the XML version
64 65 66 |
# File 'lib/lutaml/xsd/validation/xml_document.rb', line 64 def version @moxml_document.version if @moxml_document.respond_to?(:version) end |
#xpath(xpath) ⇒ Array<XmlElement>
Find elements by XPath
52 53 54 55 56 57 58 59 |
# File 'lib/lutaml/xsd/validation/xml_document.rb', line 52 def xpath(xpath) return [] unless @moxml_document.respond_to?(:xpath) nodes = @moxml_document.xpath(xpath) nodes.select(&:element?).map do |node| XmlElement.new(node, @navigator) end end |