Class: Lutaml::Xsd::Validation::XmlDocument

Inherits:
Object
  • Object
show all
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.

Examples:

Create an XML document

doc = XmlDocument.new(moxml_document, navigator)

Access root element

root = doc.root_element

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(moxml_document, navigator) ⇒ XmlDocument

Initialize a new XmlDocument

Parameters:

  • moxml_document (Moxml::Document)

    The parsed Moxml document

  • navigator (XmlNavigator)

    The navigator for this document



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_documentObject (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

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_elementsArray<XmlElement>

Get all elements in document order

Returns:



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

Parameters:

  • qualified_name (String)

    Qualified name in namespacename format

Returns:



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

#encodingString?

Get the XML encoding

Returns:

  • (String, nil)


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

#inspectString

Detailed string representation

Returns:

  • (String)


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_declarationsHash<String, String>

Get all namespace declarations in the document

Returns:

  • (Hash<String, String>)

    Map of prefix to namespace URI



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_elementXmlElement?

Get the root element of the document

Returns:



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_hHash

Convert to hash representation

Returns:

  • (Hash)


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_sString

String representation

Returns:

  • (String)


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

Returns:

  • (Boolean)


78
79
80
# File 'lib/lutaml/xsd/validation/xml_document.rb', line 78

def valid_xml?
  !@moxml_document.nil? && !root_element.nil?
end

#versionString?

Get the XML version

Returns:

  • (String, nil)


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

Parameters:

  • xpath (String)

    XPath expression

Returns:



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