Class: Moxml::Adapter::Ox
- Inherits:
-
Base
- Object
- Base
- Moxml::Adapter::Ox
show all
- Defined in:
- lib/moxml/adapter/ox.rb
Class Method Summary
collapse
-
.add_child(element, child) ⇒ Object
-
.add_next_sibling(node, sibling) ⇒ Object
-
.add_previous_sibling(node, sibling) ⇒ Object
-
.ancestors(node) ⇒ Object
-
.assign_parents(node, parent = nil) ⇒ Object
-
.at_xpath(node, expression, namespaces = {}) ⇒ Object
-
.attribute_element(attribute) ⇒ Object
-
.attributes(element) ⇒ Object
-
.cdata_content(node) ⇒ Object
-
.children(node) ⇒ Object
-
.comment_content(node) ⇒ Object
-
.create_document(native_doc = nil) ⇒ Object
-
.create_native_cdata(content, _owner_doc = nil) ⇒ Object
-
.create_native_comment(content, _owner_doc = nil) ⇒ Object
-
.create_native_declaration(version, encoding, standalone) ⇒ Object
-
.create_native_doctype(name, external_id, system_id) ⇒ Object
-
.create_native_element(name, _owner_doc = nil) ⇒ Object
-
.create_native_entity_reference(name) ⇒ Object
-
.create_native_namespace(element, prefix, uri) ⇒ Object
-
.create_native_processing_instruction(target, content) ⇒ Object
-
.create_native_text(content, _owner_doc = nil) ⇒ Object
-
.declaration_attribute(declaration, attr_name) ⇒ Object
-
.doctype_external_id(native) ⇒ Object
-
.doctype_name(native) ⇒ Object
Doctype accessor methods Ox stores DOCTYPE as a string, so we parse it.
-
.doctype_system_id(native) ⇒ Object
-
.document(node) ⇒ Object
-
.duplicate_node(node) ⇒ Object
-
.entity_reference_name(node) ⇒ Object
-
.get_attribute(element, name) ⇒ Object
-
.get_attribute_value(element, name) ⇒ Object
-
.inner_text(node) ⇒ Object
-
.namespace(element) ⇒ Object
-
.namespace_definitions(node) ⇒ Object
-
.namespace_prefix(namespace) ⇒ Object
-
.namespace_uri(namespace) ⇒ Object
-
.next_sibling(node) ⇒ Object
-
.node_name(node) ⇒ Object
-
.node_type(node) ⇒ Object
-
.parent(node) ⇒ Object
-
.parse(xml, options = {}, _context = nil) ⇒ Object
-
.patch_node(node, parent = nil) ⇒ Object
-
.previous_sibling(node) ⇒ Object
-
.processing_instruction_content(node) ⇒ Object
-
.processing_instruction_target(node) ⇒ Object
-
.remove(node) ⇒ Object
-
.remove_attribute(element, name) ⇒ Object
-
.replace(node, new_node) ⇒ Object
-
.replace_children(node, new_children) ⇒ Object
-
.root(document) ⇒ Object
-
.sax_parse(xml, handler) ⇒ void
SAX parsing implementation for Ox.
-
.serialize(node, options = {}) ⇒ Object
-
.set_attribute(element, name, value) ⇒ Object
-
.set_attribute_name(attribute, name) ⇒ Object
-
.set_attribute_value(attribute, new_value) ⇒ Object
-
.set_cdata_content(node, content) ⇒ Object
-
.set_comment_content(node, content) ⇒ Object
-
.set_declaration_attribute(declaration, attr_name, value) ⇒ Object
-
.set_namespace(element, ns) ⇒ Object
-
.set_node_name(node, name) ⇒ Object
-
.set_processing_instruction_content(node, content) ⇒ Object
-
.set_root(doc, element) ⇒ Object
-
.set_text_content(node, content) ⇒ Object
-
.text_content(node) ⇒ Object
-
.unpatch_node(node) ⇒ Object
-
.validate_single_root(document) ⇒ Object
-
.xpath(node, expression, namespaces = {}) ⇒ Object
Methods inherited from Base
create_cdata, create_comment, create_declaration, create_doctype, create_element, create_entity_reference, create_namespace, create_processing_instruction, create_text, prepare_for_new_document, sax_supported?
Methods included from XmlUtils
#encode_entities, #normalize_xml_value, #validate_comment_content, #validate_declaration_encoding, #validate_declaration_standalone, #validate_declaration_version, #validate_element_name, #validate_entity_reference_name, #validate_pi_target, #validate_prefix, #validate_uri
Class Method Details
.add_child(element, child) ⇒ Object
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
# File 'lib/moxml/adapter/ox.rb', line 364
def add_child(element, child)
if element.is_a?(::Ox::Document) && child.is_a?(::Ox::Instruct) && child.target == "xml"
element.attributes ||= {}
if child.attributes["version"]
element.attributes[:version] =
child.attributes["version"]
end
if child.attributes["encoding"]
element.attributes[:encoding] =
child.attributes["encoding"]
end
if child.attributes["standalone"]
element.attributes[:standalone] =
child.attributes["standalone"]
end
end
child.parent = element if child.is_a?(::Ox::Node)
element.nodes ||= []
element.nodes << child
if child.is_a?(::Moxml::Adapter::CustomizedOx::EntityReference)
root = element
while root.is_a?(::Ox::Node) && root.parent
root = root.parent
end
root&.instance_variable_set(:@moxml_entity_refs, true)
end
end
|
.add_next_sibling(node, sibling) ⇒ Object
408
409
410
411
412
413
414
415
416
417
|
# File 'lib/moxml/adapter/ox.rb', line 408
def add_next_sibling(node, sibling)
return unless (parent = parent(node))
if sibling.is_a?(::Ox::Node)
sibling.parent&.nodes&.delete(sibling)
sibling.parent = parent
end
idx = parent.nodes.index(node)
parent.nodes.insert(idx + 1, sibling) if idx
end
|
.add_previous_sibling(node, sibling) ⇒ Object
397
398
399
400
401
402
403
404
405
406
|
# File 'lib/moxml/adapter/ox.rb', line 397
def add_previous_sibling(node, sibling)
return unless (parent = parent(node))
if sibling.is_a?(::Ox::Node)
sibling.parent&.nodes&.delete(sibling)
sibling.parent = parent
end
idx = parent.nodes.index(node)
parent.nodes.insert(idx, sibling) if idx
end
|
.ancestors(node) ⇒ Object
168
169
170
171
172
|
# File 'lib/moxml/adapter/ox.rb', line 168
def ancestors(node)
return [] unless (parent = parent(node))
[parent] + ancestors(parent)
end
|
.assign_parents(node, parent = nil) ⇒ Object
460
461
462
463
464
465
466
467
|
# File 'lib/moxml/adapter/ox.rb', line 460
def assign_parents(node, parent = nil)
node.parent = parent if node.respond_to?(:parent=) && parent
return unless node.respond_to?(:nodes)
node.nodes&.each do |child|
assign_parents(child, node)
end
end
|
.at_xpath(node, expression, namespaces = {}) ⇒ Object
615
616
617
|
# File 'lib/moxml/adapter/ox.rb', line 615
def at_xpath(node, expression, namespaces = {})
xpath(node, expression, namespaces)&.first
end
|
.attribute_element(attribute) ⇒ Object
302
303
304
|
# File 'lib/moxml/adapter/ox.rb', line 302
def attribute_element(attribute)
attribute.parent
end
|
.attributes(element) ⇒ Object
289
290
291
292
293
294
295
296
297
298
299
300
|
# File 'lib/moxml/adapter/ox.rb', line 289
def attributes(element)
return [] unless element.is_a?(::Ox::Element) && element.attributes
element.attributes.filter_map do |name, value|
next if name.to_s.start_with?("xmlns")
::Moxml::Adapter::CustomizedOx::Attribute.new(
name.to_s, value, element
)
end
end
|
.cdata_content(node) ⇒ Object
510
511
512
|
# File 'lib/moxml/adapter/ox.rb', line 510
def cdata_content(node)
node.value.to_s
end
|
.children(node) ⇒ Object
247
248
249
250
251
252
253
254
255
256
257
|
# File 'lib/moxml/adapter/ox.rb', line 247
def children(node)
return [] unless node.is_a?(::Ox::Element) || node.is_a?(::Ox::Document)
result = node.nodes || []
result.each do |child|
child.parent = node if child.respond_to?(:parent=)
end
result
end
|
518
519
520
|
# File 'lib/moxml/adapter/ox.rb', line 518
def (node)
node.value.to_s
end
|
.create_document(native_doc = nil) ⇒ Object
66
67
68
69
|
# File 'lib/moxml/adapter/ox.rb', line 66
def create_document(native_doc = nil)
attrs = native_doc&.attributes || {}
::Ox::Document.new(**attrs)
end
|
.create_native_cdata(content, _owner_doc = nil) ⇒ Object
89
90
91
|
# File 'lib/moxml/adapter/ox.rb', line 89
def create_native_cdata(content, _owner_doc = nil)
::Ox::CData.new(content)
end
|
93
94
95
|
# File 'lib/moxml/adapter/ox.rb', line 93
def (content, _owner_doc = nil)
::Ox::Comment.new(content)
end
|
.create_native_declaration(version, encoding, standalone) ⇒ Object
109
110
111
112
113
114
115
|
# File 'lib/moxml/adapter/ox.rb', line 109
def create_native_declaration(version, encoding, standalone)
inst = ::Ox::Instruct.new("xml")
set_attribute(inst, "version", version)
set_attribute(inst, "encoding", encoding)
set_attribute(inst, "standalone", standalone)
inst
end
|
.create_native_doctype(name, external_id, system_id) ⇒ Object
97
98
99
100
101
|
# File 'lib/moxml/adapter/ox.rb', line 97
def create_native_doctype(name, external_id, system_id)
::Ox::DocType.new(
"#{name} PUBLIC \"#{external_id}\" \"#{system_id}\"",
)
end
|
.create_native_element(name, _owner_doc = nil) ⇒ Object
71
72
73
74
75
|
# File 'lib/moxml/adapter/ox.rb', line 71
def create_native_element(name, _owner_doc = nil)
element = ::Ox::Element.new(name)
element.instance_variable_set(:@attributes, {})
element
end
|
.create_native_entity_reference(name) ⇒ Object
.create_native_namespace(element, prefix, uri) ⇒ Object
125
126
127
128
129
130
|
# File 'lib/moxml/adapter/ox.rb', line 125
def create_native_namespace(element, prefix, uri)
ns = ::Moxml::Adapter::CustomizedOx::Namespace.new(prefix, uri,
element)
set_attribute(element, ns.expanded_prefix, uri)
ns
end
|
.create_native_processing_instruction(target, content) ⇒ Object
103
104
105
106
107
|
# File 'lib/moxml/adapter/ox.rb', line 103
def create_native_processing_instruction(target, content)
inst = ::Ox::Instruct.new(target)
set_processing_instruction_content(inst, content)
inst
end
|
.create_native_text(content, _owner_doc = nil) ⇒ Object
77
78
79
|
# File 'lib/moxml/adapter/ox.rb', line 77
def create_native_text(content, _owner_doc = nil)
content
end
|
.declaration_attribute(declaration, attr_name) ⇒ Object
117
118
119
|
# File 'lib/moxml/adapter/ox.rb', line 117
def declaration_attribute(declaration, attr_name)
get_attribute_value(declaration, attr_name)
end
|
.doctype_external_id(native) ⇒ Object
565
566
567
568
569
570
|
# File 'lib/moxml/adapter/ox.rb', line 565
def doctype_external_id(native)
value = native.value.to_s
match = value.match(/PUBLIC\s+"([^"]*)"/)
match ? match[1] : nil
end
|
.doctype_name(native) ⇒ Object
Doctype accessor methods Ox stores DOCTYPE as a string, so we parse it
558
559
560
561
562
563
|
# File 'lib/moxml/adapter/ox.rb', line 558
def doctype_name(native)
value = native.value.to_s.strip
value.split(/\s+/).first
end
|
.doctype_system_id(native) ⇒ Object
572
573
574
575
576
577
578
579
|
# File 'lib/moxml/adapter/ox.rb', line 572
def doctype_system_id(native)
value = native.value.to_s
matches = value.scan(/"([^"]*)"/)
matches.last&.first
end
|
.document(node) ⇒ Object
279
280
281
282
283
|
# File 'lib/moxml/adapter/ox.rb', line 279
def document(node)
current = node
current = parent(current) while parent(current)
current
end
|
.duplicate_node(node) ⇒ Object
212
213
214
|
# File 'lib/moxml/adapter/ox.rb', line 212
def duplicate_node(node)
Marshal.load(Marshal.dump(node))
end
|
.entity_reference_name(node) ⇒ Object
.get_attribute(element, name) ⇒ Object
339
340
341
342
343
344
345
346
347
348
349
350
351
|
# File 'lib/moxml/adapter/ox.rb', line 339
def get_attribute(element, name)
return unless element.is_a?(::Ox::HasAttrs) && element.attributes
unless element.attributes.key?(name.to_s) || element.attributes.key?(name.to_s.to_sym)
return
end
value = element.attributes[name.to_s] || element.attributes[name.to_s.to_sym]
::Moxml::Adapter::CustomizedOx::Attribute.new(
name.to_s, value, element
)
end
|
.get_attribute_value(element, name) ⇒ Object
353
354
355
|
# File 'lib/moxml/adapter/ox.rb', line 353
def get_attribute_value(element, name)
element[name]
end
|
.inner_text(node) ⇒ Object
495
496
497
498
499
|
# File 'lib/moxml/adapter/ox.rb', line 495
def inner_text(node)
return "" unless node.is_a?(::Ox::Element) || node.is_a?(::Ox::Document)
node.nodes.grep(String).join
end
|
.namespace(element) ⇒ Object
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/moxml/adapter/ox.rb', line 146
def namespace(element)
prefix =
if element.is_a?(::Moxml::Adapter::CustomizedOx::Attribute)
element.prefix
elsif element.name.include?(":")
element.name.split(":").first
end
attr_name = ["xmlns", prefix].compact.join(":")
([element] + ancestors(element)).each do |node|
next unless node.is_a?(::Ox::Element) && node.attributes
if node[attr_name]
return ::Moxml::Adapter::CustomizedOx::Namespace.new(
prefix, node[attr_name], element
)
end
end
nil
end
|
.namespace_definitions(node) ⇒ Object
542
543
544
545
546
547
548
549
550
551
552
553
554
|
# File 'lib/moxml/adapter/ox.rb', line 542
def namespace_definitions(node)
([node] + ancestors(node)).reverse.each_with_object({}) do |n, namespaces|
next unless n.is_a?(::Ox::Element) && n.attributes
n.attributes.each do |name, value|
next unless name.to_s.start_with?("xmlns")
namespaces[name] = ::Moxml::Adapter::CustomizedOx::Namespace.new(
name, value, n
)
end
end.values
end
|
.namespace_prefix(namespace) ⇒ Object
534
535
536
|
# File 'lib/moxml/adapter/ox.rb', line 534
def namespace_prefix(namespace)
namespace.prefix
end
|
.namespace_uri(namespace) ⇒ Object
538
539
540
|
# File 'lib/moxml/adapter/ox.rb', line 538
def namespace_uri(namespace)
namespace.uri
end
|
.next_sibling(node) ⇒ Object
263
264
265
266
267
268
269
|
# File 'lib/moxml/adapter/ox.rb', line 263
def next_sibling(node)
return unless (parent = node.parent)
siblings = parent.nodes
idx = siblings.index(unpatch_node(node))
idx ? patch_node(siblings[idx + 1], parent) : nil
end
|
.node_name(node) ⇒ Object
194
195
196
197
198
199
200
201
202
203
|
# File 'lib/moxml/adapter/ox.rb', line 194
def node_name(node)
name = begin
node.value
rescue StandardError
node.name
end
name.to_s.split(":", 2).last
end
|
.node_type(node) ⇒ Object
.parent(node) ⇒ Object
259
260
261
|
# File 'lib/moxml/adapter/ox.rb', line 259
def parent(node)
node.parent if node.is_a?(::Ox::Node)
end
|
.parse(xml, options = {}, _context = nil) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/moxml/adapter/ox.rb', line 18
def parse(xml, options = {}, _context = nil)
native_doc = begin
result = ::Ox.parse(xml)
if result.is_a?(::Ox::Document)
assign_parents(result)
validate_single_root(result) if options[:strict]
result
else
doc = ::Ox::Document.new
doc << result
assign_parents(doc)
doc
end
rescue ::Ox::ParseError => e
raise Moxml::ParseError.new(
e.message,
source: xml.is_a?(String) ? xml[0..100] : nil,
)
end
ctx = _context || Context.new(:ox)
Document.new(native_doc, ctx)
end
|
.patch_node(node, parent = nil) ⇒ Object
.previous_sibling(node) ⇒ Object
271
272
273
274
275
276
277
|
# File 'lib/moxml/adapter/ox.rb', line 271
def previous_sibling(node)
return unless (parent = parent(node))
siblings = parent.nodes
idx = siblings.index(unpatch_node(node))
idx&.positive? ? patch_node(siblings[idx - 1], parent) : nil
end
|
.processing_instruction_content(node) ⇒ Object
526
527
528
|
# File 'lib/moxml/adapter/ox.rb', line 526
def processing_instruction_content(node)
node.content.to_s
end
|
.processing_instruction_target(node) ⇒ Object
174
175
176
|
# File 'lib/moxml/adapter/ox.rb', line 174
def processing_instruction_target(node)
node.target
end
|
.remove(node) ⇒ Object
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
# File 'lib/moxml/adapter/ox.rb', line 419
def remove(node)
return node.clear if node.is_a?(String)
return unless parent(node)
if parent(node).is_a?(::Ox::Document) && node.is_a?(::Ox::Instruct) && node.target == "xml"
doc = parent(node)
doc.attributes&.delete(:version)
doc.attributes&.delete(:encoding)
doc.attributes&.delete(:standalone)
end
parent(node).nodes.delete(unpatch_node(node))
end
|
.remove_attribute(element, name) ⇒ Object
357
358
359
360
361
362
|
# File 'lib/moxml/adapter/ox.rb', line 357
def remove_attribute(element, name)
return unless element.is_a?(::Ox::HasAttrs) && element.attributes
element.attributes.delete(name.to_s)
element.attributes.delete(name.to_s.to_sym)
end
|
.replace(node, new_node) ⇒ Object
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
# File 'lib/moxml/adapter/ox.rb', line 436
def replace(node, new_node)
if node.is_a?(String) && new_node.is_a?(String)
return node.replace(new_node)
end
return unless (parent = parent(node))
new_node.parent = parent if new_node.is_a?(::Ox::Node)
idx = parent.nodes.index(node)
parent.nodes[idx] = new_node if idx
end
|
.replace_children(node, new_children) ⇒ Object
451
452
453
454
455
456
457
458
|
# File 'lib/moxml/adapter/ox.rb', line 451
def replace_children(node, new_children)
node.remove_children_by_path("*")
new_children.each do |child|
child.parent = node if child.is_a?(::Ox::Node)
node << child
end
node
end
|
.root(document) ⇒ Object
285
286
287
|
# File 'lib/moxml/adapter/ox.rb', line 285
def root(document)
document.nodes&.find { |node| node.is_a?(::Ox::Element) }
end
|
.sax_parse(xml, handler) ⇒ void
This method returns an undefined value.
SAX parsing implementation for Ox
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/moxml/adapter/ox.rb', line 49
def sax_parse(xml, handler)
bridge = OxSAXBridge.new(handler)
xml_string = xml.is_a?(IO) || xml.is_a?(StringIO) ? xml.read : xml.to_s
begin
::Ox.sax_parse(bridge, StringIO.new(xml_string))
bridge.end_document
rescue ::Ox::ParseError => e
error = Moxml::ParseError.new(e.message)
handler.on_error(error)
end
end
|
.serialize(node, options = {}) ⇒ Object
619
620
621
622
623
624
625
626
627
628
629
630
631
|
# File 'lib/moxml/adapter/ox.rb', line 619
def serialize(node, options = {})
if node.is_a?(::Ox::Document) &&
!node.instance_variable_get(:@moxml_entity_refs)
return serialize_standard(node, options)
end
if tree_has_entity_references?(node)
serialize_custom(node, options)
else
serialize_standard(node, options)
end
end
|
.set_attribute(element, name, value) ⇒ Object
306
307
308
309
310
311
312
313
314
315
316
317
318
|
# File 'lib/moxml/adapter/ox.rb', line 306
def set_attribute(element, name, value)
element.attributes ||= {}
if value.nil?
remove_attribute(element, name)
else
element.attributes[name.to_s] = value
end
::Moxml::Adapter::CustomizedOx::Attribute.new(
name.to_s, value&.to_s, element
)
end
|
.set_attribute_name(attribute, name) ⇒ Object
320
321
322
323
324
325
326
327
|
# File 'lib/moxml/adapter/ox.rb', line 320
def set_attribute_name(attribute, name)
old_name = attribute.name
attribute.name = name.to_s
element = attribute.parent
element.attributes.delete(old_name)
element.attributes[name] = attribute.value
end
|
.set_attribute_value(attribute, new_value) ⇒ Object
329
330
331
332
333
334
335
336
337
|
# File 'lib/moxml/adapter/ox.rb', line 329
def set_attribute_value(attribute, new_value)
if new_value.nil?
remove_attribute(attribute.parent, attribute.name)
else
attribute.value = new_value
attribute.parent.attributes[attribute.name] = new_value
end
end
|
.set_cdata_content(node, content) ⇒ Object
514
515
516
|
# File 'lib/moxml/adapter/ox.rb', line 514
def set_cdata_content(node, content)
node.value = content.to_s
end
|
522
523
524
|
# File 'lib/moxml/adapter/ox.rb', line 522
def (node, content)
node.value = content.to_s
end
|
.set_declaration_attribute(declaration, attr_name, value) ⇒ Object
121
122
123
|
# File 'lib/moxml/adapter/ox.rb', line 121
def set_declaration_attribute(declaration, attr_name, value)
set_attribute(declaration, attr_name, value)
end
|
.set_namespace(element, ns) ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/moxml/adapter/ox.rb', line 132
def set_namespace(element, ns)
return unless element.is_a?(::Ox::Element) || element.is_a?(::Ox::Node)
prefix = ns.prefix
if element.is_a?(::Ox::Element)
set_attribute(element, ns.expanded_prefix,
ns.uri)
end
element.name = [prefix,
element.name.delete_prefix("xmlns:")].compact.join(":")
namespace(element)
end
|
.set_node_name(node, name) ⇒ Object
205
206
207
208
209
210
|
# File 'lib/moxml/adapter/ox.rb', line 205
def set_node_name(node, name)
case node
when ::Ox::Element then node.name = name
when ::Ox::Instruct then node.value = name
end
end
|
.set_processing_instruction_content(node, content) ⇒ Object
530
531
532
|
# File 'lib/moxml/adapter/ox.rb', line 530
def set_processing_instruction_content(node, content)
node.content = content.to_s
end
|
.set_root(doc, element) ⇒ Object
14
15
16
|
# File 'lib/moxml/adapter/ox.rb', line 14
def set_root(doc, element)
replace_children(doc, [element])
end
|
.set_text_content(node, content) ⇒ Object
501
502
503
504
505
506
507
508
|
# File 'lib/moxml/adapter/ox.rb', line 501
def set_text_content(node, content)
case node
when String then node.replace(content.to_s)
when ::Ox::Element then node.replace_text(content.to_s)
else
node.value = content.to_s
end
end
|
.text_content(node) ⇒ Object
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
# File 'lib/moxml/adapter/ox.rb', line 479
def text_content(node)
return "" if node.nil?
case node
when String then node.to_s
when ::Moxml::Adapter::CustomizedOx::Text then node.value
when ::Moxml::Adapter::CustomizedOx::EntityReference then ""
else
return "" unless node.is_a?(::Ox::Element) || node.is_a?(::Ox::Document)
node.nodes.map do |n|
text_content(n)
end.join
end
end
|
.unpatch_node(node) ⇒ Object
.validate_single_root(document) ⇒ Object
469
470
471
472
473
474
475
476
477
|
# File 'lib/moxml/adapter/ox.rb', line 469
def validate_single_root(document)
elements = document.nodes&.grep(::Ox::Element) || []
return unless elements.size > 1
raise Moxml::ParseError.new(
"Multiple root elements found",
source: nil,
)
end
|
.xpath(node, expression, namespaces = {}) ⇒ Object
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
|
# File 'lib/moxml/adapter/ox.rb', line 581
def xpath(node, expression, namespaces = {})
locate_expr = translate_xpath_to_locate(expression, namespaces)
if expression.start_with?(".//") && node.is_a?(::Ox::Element)
element_name = locate_expr.sub("?/", "")
results = []
traverse(node) do |n|
next unless n.is_a?(::Ox::Element)
results << n if n.name == element_name || element_name.empty?
end
return results.map do |n|
patch_node(n, find_parent_in_tree(n, node))
end
end
results = node.locate(locate_expr)
results.map { |n| patch_node(n, find_parent_in_tree(n, node)) }
rescue StandardError => e
raise Moxml::XPathError.new(
"XPath translation failed: #{e.message}",
expression: expression,
adapter: "Ox",
node: node,
)
end
|