Class: Mixml::Selection

Inherits:
Object
  • Object
show all
Defined in:
lib/mixml/selection.rb

Overview

Selection of XML nodes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodesets) ⇒ Selection

Initialize a new selection

Parameters:

  • nodesets (Array<Nokogiri::XML::NodeSet>)

    Selected nodes



13
14
15
# File 'lib/mixml/selection.rb', line 13

def initialize(nodesets)
    @nodesets = nodesets
end

Instance Attribute Details

#nodesetsNokogiri::XML::NodeSet (readonly)

Returns Selected nodes.

Returns:

  • (Nokogiri::XML::NodeSet)

    Selected nodes



8
9
10
# File 'lib/mixml/selection.rb', line 8

def nodesets
  @nodesets
end

Instance Method Details

#append(template) ⇒ Object

Append children to node

Parameters:



57
58
59
60
61
62
63
64
# File 'lib/mixml/selection.rb', line 57

def append(template)
    template = template.to_mixml_template

    each_node do |node|
        value = template.evaluate(node)
        node << value
    end
end

#removeObject

Remove selected nodes from the document



18
19
20
21
22
# File 'lib/mixml/selection.rb', line 18

def remove
    each_node do |node|
        node.remove
    end
end

#rename(template) ⇒ Object

Rename selected nodes with a template

Parameters:



81
82
83
84
85
86
87
88
# File 'lib/mixml/selection.rb', line 81

def rename(template)
    template = template.to_mixml_template

    each_node do |node|
        value = template.evaluate(node)
        node.name = value
    end
end

#replace(template) ⇒ Object

Replace selected nodes with a template

Parameters:



45
46
47
48
49
50
51
52
# File 'lib/mixml/selection.rb', line 45

def replace(template)
    template = template.to_mixml_template

    each_node do |node|
        value = template.evaluate(node)
        node.replace(value)
    end
end

#value(template) ⇒ Object

Set the value selected nodes with a template

Parameters:



69
70
71
72
73
74
75
76
# File 'lib/mixml/selection.rb', line 69

def value(template)
    template = template.to_mixml_template

    each_node do |node|
        value = template.evaluate(node)
        node.value = value
    end
end

#write(template = nil) ⇒ Object

Print selected nodes to stdout

Parameters:

  • template (Template::Base) (defaults to: nil)

    Template to evaluate and print



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mixml/selection.rb', line 27

def write(template = nil)
    if not template.nil? then
        template = template.to_mixml_template
    end

    each_node do |node|
        if template.nil? then
            node.write_xml_to($stdout)
            puts
        else
            puts template.evaluate(node)
        end
    end
end