The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

XML::Tidy - tidy indenting of XML documents

VERSION

This documentation refers to version 1.2.51HM2ae of XML::Tidy, which was released on Mon Jan 17 22:02:36:40 2005.

SYNOPSIS

  use XML::Tidy;

  # create new   XML::Tidy object from         MainFile.xml
  my $tidy_obj = XML::Tidy->new('filename' => 'MainFile.xml');

  # Tidy up the indenting
     $tidy_obj->tidy();

  # Write out changes back to MainFile.xml
     $tidy_obj->write();

DESCRIPTION

This module creates XML document objects (with inheritance from XML::XPath) to tidy mixed-content (ie. non-data) text node indenting.

2DO

- add tests for XML::XPath::Node wrappers
- add tests for toString
- fix compress() comment bug
- add tests for compress() && expand()
- imp namespace option in compress()
- mk tidy keep doc order when duping attz, namespaces, (hopefully someday PIs) into temp $docu && $tnod
- What else does Tidy need?

USAGE

new()

This is the standard Tidy object constructor. It can take the same parameters as an XML::XPath object constructor to initialize the XML document object. These can be any one of:

  'filename' => 'SomeFile.xml'
  'xml'      => $variable_which_holds_a_bunch_of_XML_data
  'ioref'    => $file_InputOutput_reference
  'context'  => $existing_node_at_specified_context_to_become_new_obj

reload()

The reload() member function causes the latest data contained in a Tidy object to be re-parsed which re-indexes all nodes. This can be necessary after modifications have been made to nodes which impact the tree node hierarchy because XML::XPath's find() member preserves state info which can get out-of-sync. reload() is probably rarely useful by itself but it is needed by strip() && prune() so it is exposed as a method in case it comes in handy for other uses.

strip()

The strip() member function searches the Tidy object for all mixed-content (ie. non-data) text nodes && empties them out. This will basically unformat any markup indenting. strip() is used by compress() && tidy() but it is exposed because it could be worthwhile by itself.

tidy()

The tidy() member function can take a single optional parameter as the string that should be inserted for each indent level. Some examples:

  # Tidy up indenting with default two  (2) spaces per indent level
     $tidy_obj->tidy();

  # Tidy up indenting with         four (4) spaces per indent level
     $tidy_obj->tidy('    ');

  # Tidy up indenting with         one  (1) tab    per indent level
     $tidy_obj->tidy("\t");

The default behavior is to use two (2) spaces (ie. ' ') for each indent level. The Tidy object gets all mixed-content (ie. non-data) text nodes reformatted to appropriate indent levels according to tree nesting depth.

NOTE: There seems to be a bug in XML::XPath which does not allow finding XML processing instructions (PIs) properly so they have been commented out of tidy(). This means that tidy() unfortunately removes processing instructions from files it operates on. I hope this shortcoming can be repaired in the near future. tidy() also disturbs some XML escapes in whatever ways XML::XPath does.

compress()

The compress() member function calls strip() on the Tidy object then creates comments ahead of the root element which contain the names of elements && attributes as they occurred with their respective element && attribute names represented as just an index throughout the document.

compress() can accept a parameter describing which node types to attempt to shrink down as abbreviations. This parameter should be a string of just the first letters of each node type you wish to include as in the following mapping:

  e = elements
  a = attribute keys
  v = attribute values *EXPERIMENTAL*
  t = text      nodes  *EXPERIMENTAL*
  c = comment   nodes  *EXPERIMENTAL*
  n = namespace nodes  *not-yet-implemented*

Attribute values ('v') && text nodes ('t') both seem to work fine as they are tokenized. I have some bugs in the comment node compression which I haven't been able to find yet so that one should be avoided for now. Since these three node types ('vtc') all require tokenization, they are not included in default compression ('ea'). An example call which includes values && text would be:

  $tidy_obj->compress('eatv');

The original document structure (ie. node hierarchy) is preserved. compress() significantly reduces the file size of most XML documents for when size matters more than immediate human readability. expand() performs the opposite conversion.

expand()

The expand() member function reads any XML::Tidy::compress comments from the Tidy object && uses them to reconstruct the document that was passed to compress(). These utilities together seem like a mildly useful way to tidy XML documents so they earned inclusion in this module. compress() && expand() should be considered experimental.

prune()

The prune() member function takes an XPath location to remove (along with all attributes && child nodes) from the Tidy object. For example, to remove all comments:

  $tidy_obj->prune('//comment()');

or to remove the third baz (XPath indexing is 1-based):

  $tidy_obj->prune('/foo/bar/baz[3]');

Pruning your XML tree is a form of tidying too so it snuck in here. =) It seems XML::XPath objects are dramatically more useful when they all have access to this class of additional member functions.

write()

The write() member function can take an optional filename parameter to write out any changes to the Tidy object. If no parameters are given, write() overwrites the original XML document file (if a 'filename' parameter was given to the constructor).

write() will croak() if no filename can be found to write to.

write() can also take a secondary parameter which specifies an XPath location to be written out as the new root element instead of the Tidy object's root. Only the first matching element is written.

toString()

The toString() member function is almost identical to write() except that it takes no parameters && simply returns the equivalent XML string as a scalar. It is a little weird because normally only XML::XPath::Node objects have a toString member but I figure it makes sense to extend the same syntax to the parent object as well since it is a useful option.

createNode Wrappers

The following are just aliases to Node constructors. They'll work with just the unique portion of the node type as the member function name.

e() or el() or elem() or createElement()

wrapper for XML::XPath::Node::Element->new()

a() or at() or attr() or createAttribute()

wrapper for XML::XPath::Node::Attribute->new()

c() or cm() or cmnt() or createComment()

wrapper for XML::XPath::Node::Comment->new()

t() or tx() or text() or createTextNode()

wrapper for XML::XPath::Node::Text->new()

p() or pi() or proc() or createProcessingInstruction()

wrapper for XML::XPath::Node::PI->new()

n() or ns() or nspc() or createNamespace()

wrapper for XML::XPath::Node::Namespace->new()

EXPORTED CONSTANTS

XML::Tidy also exports the same node constants as XML::XPath::Node (which correspond to DOM values). These include:

  UNKNOWN_NODE
  ELEMENT_NODE
  ATTRIBUTE_NODE
  TEXT_NODE
  CDATA_SECTION_NODE
  ENTITY_REFERENCE_NODE
  ENTITY_NODE
  PROCESSING_INSTRUCTION_NODE
  COMMENT_NODE
  DOCUMENT_NODE
  DOCUMENT_TYPE_NODE
  DOCUMENT_FRAGMENT_NODE
  NOTATION_NODE
  ELEMENT_DECL_NODE
  ATT_DEF_NODE
  XML_DECL_NODE
  ATTLIST_DECL_NODE
  NAMESPACE_NODE

XML::Tidy also exports:

  STANDARD_XML_DECL

which returns a reasonable default XML declaration string.

CHANGES

Revision history for Perl extension XML::Tidy:

- 1.2.51HM2ae Mon Jan 17 22:02:36:40 2005

* added compress() && expand()

* added toString()

- 1.2.4CKBHxt Mon Dec 20 11:17:59:55 2004

* added exporting of XML::XPath::Node (DOM) constants

* added node object creation wrappers (like LibXML)

- 1.2.4CCJW4G Sun Dec 12 19:32:04:16 2004

* added optional 'xpath_loc' => to prune()

- 1.0.4CAJna1 Fri Dec 10 19:49:36:01 2004

* added optional 'filename' => to write()

- 1.0.4CAAf5B Fri Dec 10 10:41:05:11 2004

* removed 2nd param from tidy() so that 1st param is just indent string

* fixed pod errors

- 1.0.4C9JpoP Thu Dec 9 19:51:50:25 2004

* added xplc option to write()

* added prune()

- 1.0.4C8K1Ah Wed Dec 8 20:01:10:43 2004

* inherited from XPath so that those methods can be called directly

* original version (separating Tidy.pm from Merge.pm)

INSTALL

From the command shell, please run:

    `perl -MCPAN -e "install XML::Tidy"`

or uncompress the package && run the standard:

    `perl Makefile.PL; make; make test; make install`

FILES

XML::Tidy requires:

Carp to allow errors to croak() from calling sub

XML::XPath to use XPath statements to query && update XML

XML::XPath::XMLParser to parse XML documents into XPath objects

LICENSE

Most source code should be Free! Code I have lawful authority over is && shall be! Copyright: (c) 2004, Pip Stuart. Copyleft : This software is licensed under the GNU General Public License (version 2), && as such comes with NO WARRANTY. Please consult the Free Software Foundation (http://FSF.Org) for important information about your freedom.

AUTHOR

Pip Stuart <Pip@CPAN.Org>

1 POD Error

The following errors were encountered while parsing the POD:

Around line 51:

Non-ASCII character seen before =encoding in '©'. Assuming CP1252