The PTree File Format

Saxon-PE and Saxon-EE support a file format called the PTree (persistent tree). This is a binary representation of an XML document. The PTree file is generally about the same size as the original document (perhaps 10% smaller), but it typically loads in about half the time. Storing a document as a PTree can therefore give a useful performance improvement when the same source document is used repeatedly as the input to many queries or transformations. Another benefit of the PTree is that it retains any type information that is present, which means that the document does not need to be validated against its schema each time it is loaded. (The schema, however, must be loaded whenever the document is loaded.)

Two commands are available for converting XML documents into PTree files and vice versa. To create a PTree, use:

java  com.saxonica.ptree.PTreeWriter source.xml result.ptree

The option -strip causes all whitespace-only text nodes to be stripped in the process, which will often give a useful saving in space and therefore in loading time.

To convert a PTree back to an XML document, use:

java  com.saxonica.ptree.PTreeReader source.ptree result.xml

It is possible to apply a query or transformation directly to a PTree by specifying the -p option on the command line for com.saxonica.Transform or com.saxonica.Query. This option actually causes a different URIResolver, the PTreeURIResolver, to be used in place of the standard URIResolver. The PTreeURIResolver recognizes any URI ending in the extension .ptree as an identifier for a file in PTree format. This extends to files loaded using the doc() or document() functions: if the file extension is .ptree, the file will be assumed to be in PTree format.

The result of a query or transformation can be serialized as a PTree file by specifying saxon:ptree as the output method, where the namespace prefix saxon represents the URI

The PTree format is designed to allow future Saxon releases to read files created using older releases. The converse may not always be true: it might sometimes be impossible for release N to read a PTree file created using release N+1.

In releases up to and including Saxon 9.8 the PTree files were always at version 0. A change was introduced in Saxon 9.4 to retain DTD-derived attribute types (ID, IDREF, IDREFS), but (despite the documentation saying otherwise) this did not involve a change of version number, and all releases were able to read all versions.

In Saxon 9.9 a change has been introduced to allow extra compression for elements that contain no attributes, no namespace declarations, and a single text node. Saxon 9.9 therefore writes files as version=1. It can continue to read version 0 files, but it cannot write version 0 files, and earlier releases cannot read version 1 files. This means that a PTree file written by Saxon 9.9 cannot be read by earlier releases.

The PTree format does not retain the base URI of the original file: when a PTree is loaded, the base URI is taken as the URI of that file, not the original XML file. The PTree is a serialization of the XPath data model, so information that isn't present in the data model will not be present in the PTree: for example, it will have no DTD and no entity references or CDATA sections.

References to unparsed entities are not currently retained in a PTree.