VERSION

This documentation refers to version 1.12.B55J2qn of XML::Tidy, which was released on Thu May 5 19:02:52:49 2011.

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 \s-1XML\s0 document objects (with inheritance from XML::XPath) to tidy mixed-content (i.e., non-data) text node indenting. There are also some other handy member functions to compress && expand your \s-1XML\s0 document object (into either a compact \s-1XML\s0 representation or a binary one).

2DU

- maybe add support for binary char && short ints && single-precision floats
- maybe store recurring patterns of node index sets in new array that can be indexed themself
- fix reload() from messing up unicode escaped &XYZ; components like Copyright © -> X && Registered ® -> X
- What else does Tidy need?

USAGE

\fInew()\fP

This is the standard Tidy object constructor. Except for the new 'binary' option, it can take the same parameters as an XML::XPath object constructor to initialize the \s-1XML\s0 document object. These can be any one of:

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

\fIreload()\fP

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 information 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.

\fIstrip()\fP

The strip() member function searches the Tidy object for all mixed-content (i.e., 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.

\fItidy()\fP

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 for each indent level. The Tidy object gets all mixed-content (i.e., non-data) text nodes reformatted to appropriate indent levels according to tree nesting depth.

\s-1NOTE:\s0 tidy() disturbs some \s-1XML\s0 escapes in whatever ways XML::XPath does. It has been brought to my attention that these modules also strip \s-1CDATA\s0 tags from \s-1XML\s0 files / data they operate on. Even though \s-1CDATA\s0 tags don't seem very common, I wish they could work smoothly too. Hopefully the vast majority of files will work fine && support for other types can be added later.

\fIcompress()\fP

The compress() member function calls strip() on the Tidy object then creates an encoded comment which contains the names of elements && attributes as they occurred in the original document. Their respective element && attribute names are replaced with just the appropriate 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 with current tokenization. I've still labeled them \s-1EXPERIMENTAL\s0 because they seem more likely to cause problems than valid element or attribute key names. 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('eavt');

The original document structure (i.e., node hierarchy) is preserved. compress() significantly reduces the file size of most \s-1XML\s0 documents for when size matters more than immediate human readability. expand() performs the opposite conversion.

\fIexpand()\fP

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().

bcompress('BinaryOutputFilename.xtb')

The bcompress() member function stores a binary representation of any Tidy object. The format consists of:

0) a null-terminated version string 1) a byte specifying how many bytes later indices will be 2) the number of bytes from 1 above to designate the total string count 3) the number of null-terminated strings from 2 above 4) the number of bytes from 1 above to designate the total integer count 5) the number of 4-byte integers from 4 above 6) the number of bytes from 1 above to designate the total float count 7) the number of 8-byte (double-precision) floats from 6 above 8) node index sets until the end of the file

Normal node index sets consist of two values. The first is an index (again the number of bytes long comes from 1) into the three lists as if they were all linear. The second is a single-byte integer identifying the node type (using standard \s-1DOM\s0 node type enumerations).

A few special cases exist in node index sets though. If the index is null, it is interpreted as a close-element tag (so no accompanying type value is read). On the other end, when the index is non-zero, the type value is always read. In the event that the type corresponds to an attribute or a processing instruction, the next index is read (without another accompanying type value) in order to complete the data fields required by those node types.

\s-1NOTE:\s0 Please bear in mind that the encoding of binary integers && floats only works properly if the values are not surrounded by spaces or other delimiters && each is contained in its own single node. This is necessary to enable thorough reconstruction of whitespace from the original document. I recommend storing every numerical value as an isolated attribute value or text node without any surrounding whitespace.

# Examples which encode all numbers as binary: <friend name="goodguy" category="15"> <hitpoints>31.255</hitpoints> <location> <x>-15.65535</x> <y>16383.7</y> <z>-1023.63</z> </location> </friend>

# Examples which encode all numbers as strings: <enemy name="badguy" category=" 666 "> <hitpoints> 2.0 </hitpoints> <location> 4.0 -2.0 4.0 </location> </enemy>

The default file extension is .xtb (for XML::Tidy binary).

bexpand('BinaryInputFilename.xtb')

The bexpand() member function reads a binary file which was previously written from bcompress(). bexpand() is an XML::Tidy object constructor like new() so it can be called like:

my $xtbo = XML::Tidy->bexpand('BinaryInputFilename.xtb');

\fIprune()\fP

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 \s-1XML\s0 tree is a form of tidying too so it snuck in here. =)

\fIwrite()\fP

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 \s-1XML\s0 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.

\fItoString()\fP

The toString() member function is almost identical to write() except that it takes no parameters && simply returns the equivalent \s-1XML\s0 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 \fIel()\fP or \fIelem()\fP or \fIcreateElement()\fP

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

a() or \fIat()\fP or \fIattr()\fP or \fIcreateAttribute()\fP

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

c() or \fIcm()\fP or \fIcmnt()\fP or \fIcreateComment()\fP

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

t() or \fItx()\fP or \fItext()\fP or \fIcreateTextNode()\fP

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

p() or \fIpi()\fP or \fIproc()\fP or \fIcreateProcessingInstruction()\fP

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

n() or \fIns()\fP or \fInspc()\fP or \fIcreateNamespace()\fP

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

EXPORTED CONSTANTS

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

\s-1UNKNOWN_NODE\s0

\s-1ELEMENT_NODE\s0

\s-1ATTRIBUTE_NODE\s0

\s-1TEXT_NODE\s0

\s-1CDATA_SECTION_NODE\s0

\s-1ENTITY_REFERENCE_NODE\s0

\s-1ENTITY_NODE\s0

\s-1PROCESSING_INSTRUCTION_NODE\s0

\s-1COMMENT_NODE\s0

\s-1DOCUMENT_NODE\s0

\s-1DOCUMENT_TYPE_NODE\s0

\s-1DOCUMENT_FRAGMENT_NODE\s0

\s-1NOTATION_NODE\s0

\s-1ELEMENT_DECL_NODE\s0

\s-1ATT_DEF_NODE\s0

\s-1XML_DECL_NODE\s0

\s-1ATTLIST_DECL_NODE\s0

\s-1NAMESPACE_NODE\s0

XML::Tidy also exports:

\s-1STANDARD_XML_DECL\s0

which returns a reasonable default \s-1XML\s0 declaration string.

CHANGES

Revision history for Perl extension XML::Tidy:

- 1.12.B55J2qn Thu May 5 19:02:52:49 2011

* made \*(L"1.0\*(R" float binarize as float again, rather than just \*(L"1\*(R" int * cleaned up \s-1POD\s0 && fixed \s-1EXPORTED\s0 \s-1CONSTANTS\s0 heads blocking together

- 1.10.B52FpLx Mon May 2 15:51:21:59 2011

* added tests for undefined non-standard \s-1XML\s0 declaration to suppress warnings

- 1.8.B2AMvdl Thu Feb 10 22:57:39:47 2011

* aligned .t code * added test for newline before -r to try to resolve: \s-1HTTPS://RT\s0.CPAN.Org/Ticket/Display.html?id=65471 (Thanks, Leandro.) * fixed off-by-one error when new gets a readable (non-newline) filename (that's not \*(L"filename\*(R" without a pre-'filename' param) to resolve: \s-1HTTPS://RT\s0.CPAN.Org/Ticket/Display.html?id=65151 (Thanks, Simone.)

- 1.6.A7RJKwl Tue Jul 27 19:20:58:47 2010

* added head2 \s-1POD\s0 for \s-1EXPORTED\s0 \s-1CONSTANTS\s0 to try to pass t/00podc.t

- 1.4.A7QCvHw Mon Jul 26 12:57:17:58 2010

* hacked a little test for non-UTF-8 decl str to resolve FrankGoss' need for \s-1ISO-8859-1\s0 decl encoding to persist through tidying * md sure \s-1META\s0.yml is being generated correctly for the \s-1CPAN\s0 * updated license to GPLv3

- 1.2.75BACCB Fri May 11 10:12:12:11 2007

* made \*(L"1.0\*(R" float binarize as just \*(L"1\*(R" int * made ints signed && bounds checked * added new('binary' => 'BinFilename.xtb') option

- 1.2.54HJnFa Sun Apr 17 19:49:15:36 2005

* fixed tidy() processing instruction stripping problem * added support for binary ints && floats in bcompress() * tightened up binary format && added pod

- 1.2.54HDR1G Sun Apr 17 13:27:01:16 2005

* added bcompress() && bexpand() * added compress() && expand() * added toString()

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

* added exporting of XML::XPath::Node (\s-1DOM\s0) 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 \s-1XML\s0

XML::XPath::XMLParser to parse \s-1XML\s0 documents into XPath objects

Math::BaseCnv to handle base-64 indexing for compress() && expand()

LICENSE

Most source code should be Free!

  Code I have lawful authority over is && shall be!

Copyright: (c) 2004-2011, Pip Stuart. Copyleft : This software is licensed under the \s-1GNU\s0 General Public

  License (version 3). Please consult the Free Software Foundation
  (\s-1HTTP://FSF\s0.Org) for important information about your freedom.

AUTHOR

Pip Stuart <[email protected]>