Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Contact Us
Online Help
Domain Status
Man Pages

Virtual Servers

Topology Map

Server Agreement
Year 2038

USA Flag



Man Pages

Manual Reference Pages  -  XML::FILTER::REINDENT (3)

.ds Aq ’


XML::Filter::Reindent - Reformats whitespace for pretty printing XML



 use XML::Handler::Composer;
 use XML::Filter::Reindent;

 my $composer = new XML::Handler::Composer (%OPTIONS);
 my $indent = new XML::Filter::Reindent (Handler => $composer, %OPTIONS);


XML::Filter::Reindent is a sub class of XML::Filter::DetectWS.

XML::Filter::Reindent can be used as a PerlSAX filter to reformat an XML document before sending it to a PerlSAX handler that prints it (like XML::Handler::Composer.)

Like XML::Filter::DetectWS, it detects ignorable whitespace and blocks of whitespace characters in certain places. It uses this information and information supplied by the user to determine where whitespace may be modified, deleted or inserted. Based on the indent settings, it then modifies, inserts and deletes characters and ignorable_whitespace events accordingly.

This is just a first stab at the implementation. It may be buggy and may change completely!

Constructor Options

o Handler

The PerlSAX handler (or filter) that will receive the PerlSAX events from this filter.

o Tab (Default: one space)

The number of spaces per indent level for elements etc. in document content.

o Newline (Default: \x0A)

The newline to use when re-indenting. The default is the internal newline used by XML::Parser, XML::DOM etc., and should be fine when used in combination with XML::Handler::Composer.

$self->indent_children ($start_element_event)

This method determines whether children of a certain element may be reformatted. The default implementation checks the PreserveWS parameter of the specified start_element event and returns 0 if it is set or MAYBE otherwise. The value MAYBE \fIs0(2) indicates that further investigation is needed, e.g. by examining the element contents. A value of 1 means yes, indent the child nodes, no further investigation is needed.

NOTE: the PreserveWS parameter is set by the parent class, XML::Filter::DetectWS, when the element or one of its ancestors has the attribute xml:space=preserve.

Override this method to tweak the behavior of this class.

$self->indent_element ($start_element_event, $parent_says_indent)

This method determines whether a certain element may be re-indented. The default implementation returns the value of the $parent_says_indent parameter, which was set to the value returned by indent_children for the parent element. In other words, the element will be re-indented if the parent element allows it.

Override this method to tweak the behavior of this class. I’m not sure how useful this hook is. Please provide feedback!

Current Implementation

The current implementation puts all incoming Perl SAX events in a queue for further processing. When determining which nodes should be re-indented, it sometimes needs information from previous events, hence the use of the queue.

The parameter (Compress => 1) is added to matching start_element and end_element events with no events in between This indicates to an XML printer that a compressed notation can be used, e.g <foo/>.

If an element allows reformatting of its contents (xml:space=preserve was not active and indent_children returned MAYBE), the element contents will be reformatted unless it only has one child node and that child is a regular text node (characters event.) In that case, the element will be printed as <foo>text contents</foo>.

If you want element nodes with just one text child to be reindented as well, simply override indent_children to return 1 instead of MAYBE (2.)

This behavior may be changed or extended in the future.


This code is highly experimental! It has not been tested well and the API may change.

The code that detects blocks of whitespace at potential indent positions may need some work.


Send bug reports, hints, tips, suggestions to Enno Derksen at <>.
Search for    or go to Top of page |  Section 3 |  Main Index

perl v5.20.3 XML::FILTER::REINDENT (3) 2000-02-16

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.