| Class | REXML::Formatters::Transitive |
| In: |
lib/rexml/formatters/transitive.rb
|
| Parent: | Default |
The Transitive formatter writes an XML document that parses to an identical document as the source document. This means that no extra whitespace nodes are inserted, and whitespace within text nodes is preserved. Within these constraints, the document is pretty-printed, with whitespace inserted into the metadata to introduce formatting.
Note that this is only useful if the original XML is not already formatted. Since this formatter does not alter whitespace nodes, the results of formatting already formatted XML will be odd.
# File lib/rexml/formatters/transitive.rb, line 15
15: def initialize( indentation=2 )
16: @indentation = indentation
17: @level = 0
18: end
# File lib/rexml/formatters/transitive.rb, line 21
21: def write_element( node, output )
22: output << "<#{node.expanded_name}"
23:
24: node.attributes.each_attribute do |attr|
25: output << " "
26: attr.write( output )
27: end unless node.attributes.empty?
28:
29: output << "\n"
30: output << ' '*@level
31: if node.children.empty?
32: output << "/"
33: else
34: output << ">"
35: # If compact and all children are text, and if the formatted output
36: # is less than the specified width, then try to print everything on
37: # one line
38: skip = false
39: @level += @indentation
40: node.children.each { |child|
41: write( child, output )
42: }
43: @level -= @indentation
44: output << "</#{node.expanded_name}"
45: output << "\n"
46: output << ' '*@level
47: end
48: output << ">"
49: end