Handling XSL:FO’s memory issue with large page counts

ScriptoriumTech / XMLLeave a Comment

Formatting Object (FO) processors (FOP, in particular) often fail with memory errors when processing very large documents for PDF output. Typically in XSL:FO, the body of a document is contained in a single fo:page-sequence element. When FO documents are converted to PDF output, the FO processor holds an entire fo:page-sequence in memory to perform pagination adjustments over the span of the sequence. Very large page counts can result in memory overflows or Java heap space errors. Reducing page count in a document is not usually an option.

You can set up processing to divide the document into multiple fo:page sequences to avoid memory problems. The granularity of the content of each fo:page-sequence does not matter. Each fo:page-sequence could contain a chapter, a sub-chapter, a section, and so on. For a DITA implementation, each DITA topic could be placed in its own page sequence. The key is to place elements in a flat sequence of fo:page-sequences.

Flattening the hierarchy is challenging. The examples in this document will explain the approach.

NOTE: This document assumes basic familiarity with XSL, XSL:FO, DITA, and the DITA Open Toolkit.

Considerations in creating multiple page sequences

One problem with modifying page sequences is that fo:page-sequences cannot contain other fo:page-sequences. The XSL that creates the FO output must chunk the content and organize the chunks as peers at the level where the chunks will be divided into fo:page-sequences.

If your chunks are chapters, they are probably already peers. Inside the chapters, the organization of elements can remain the same. The chapter chunks need to be wrapped in their own fo:page-sequence elements.

Another issue in creating multiple page sequences is that each fo:page-sequence forces the start of a new page. This is probably not an issue if you divide a book into one fo:page-sequence per chapter, but putting very short sections inside separate fo:page-sequences would lead to a lot of empty paper.

Creating a sequential set of output based on a hierarchically ordered set of inputs might suggest that there would be a change in formatting. Formatting issues are not a problem when applying this approach to the DITA Open Toolkit (OT). The division of page sequences does not affect numbering streams because those streams are dependent on the organization of the input document—not the output document. Cross-references are typically based on positions in the input document; placement in separate page sequences does not affect the resolution of fo:internal-destination elements in the output document.

If indentations for elements at different levels in the hierarchy are always relative to the page margin, the indentation is not dependent on the relationship of child blocks to parent blocks in the output. The relationship of these blocks can be altered without changing the physical indentation.

This kind of relationship turns out, in fact, to be the case for the DITA OT. No changes to indentations are required in the DITA OT for this solution. For other implementations, you may need to adjust formatting so indentation is not dependent on the hierarchical relationships of blocks in the output.

XML document structure and fo:page-sequences

In an XML document, you might have a structure similar to Example 1.

Example 1

[code]]czo2OlwiPGJvb2s+XCI7e1smKiZdfQ==[[/code] [code]]czoxMzpcIiAgICA8c2VjdGlvbj5cIjt7WyYqJl19[[/code] [code]]czoyMDpcIiAgICA8Ym9keT4uLi48L2JvZHk+XCI7e1smKiZdfQ==[[/code] [code]]czoxNTpcIiAgICAgIDxzZWN0aW9uPlwiO3tbJiomXX0=[[/code] [code]]czoyMjpcIiAgICAgICAgPGJvZHk+IDwvYm9keT5cIjt7WyYqJl19[[/code] [code]]czoxNjpcIiAgICAgIDwvc2VjdGlvbj5cIjt7WyYqJl19[[/code] [code]]czoxNDpcIiAgICA8L3NlY3Rpb24+XCI7e1smKiZdfQ==[[/code] [code]]czo3OlwiPC9ib29rPlwiO3tbJiomXX0=[[/code]

Now imagine that the second-level sections contain hundreds of additional sections, many embedded in each other, and the document is 10,000 pages long.

The FO output for the body of the example document is similar to Example 2:

Example 2

[code]]czoxODpcIjxmbzpwYWdlLXNlcXVlbmNlPlwiO3tbJiomXX0=[[/code] [code]]czoxMzpcIiAgICA8Zm86Zmxvdz5cIjt7WyYqJl19[[/code] [code]]czozMDpcIiAgICAgIDxmbzpibG9jaz4gPCEtLSBib29rIC0tPlwiO3tbJiomXX0=[[/code] [code]]czozNTpcIiAgICAgICAgPGZvOmJsb2NrPiA8IS0tIHNlY3Rpb24gLS0+XCI7e1smKiZdfQ==[[/code] [code]]czo0MTpcIiAgICAgICAgICA8Zm86YmxvY2s+IDwhLS0gc2VjdGlvbiBib2R5LS0+XCI7e1smKiZdfQ==[[/code] [code]]czoyMTpcIiAgICAgICAgICA8L2ZvOmJsb2NrPlwiO3tbJiomXX0=[[/code] [code]]czo0MzpcIiAgICAgICAgICA8Zm86YmxvY2s+IDwhLS0gY2hpbGQgc2VjdGlvbiAtLT5cIjt7WyYqJl19[[/code] [code]]czo1MTpcIiAgICAgICAgICAgIDxmbzpibG9jaz4gPCEtLSBjaGlsZCBzZWN0aW9uIGJvZHkgLS0+IFwiO3tbJiomXX0=[[/code] [code]]czoyMzpcIiAgICAgICAgICAgIDwvZm86YmxvY2s+XCI7e1smKiZdfQ==[[/code] [code]]czoyMTpcIiAgICAgICAgICA8L2ZvOmJsb2NrPlwiO3tbJiomXX0=[[/code] [code]]czoxOTpcIiAgICAgICAgPC9mbzpibG9jaz5cIjt7WyYqJl19[[/code] [code]]czoxNzpcIiAgICAgIDwvZm86YmxvY2s+XCI7e1smKiZdfQ==[[/code] [code]]czoxNDpcIiAgICA8L2ZvOmZsb3c+XCI7e1smKiZdfQ==[[/code] [code]]czoxOTpcIjwvZm86cGFnZS1zZXF1ZW5jZT5cIjt7WyYqJl19[[/code]

To break the document into multiple page sequences down to the second-level section, the output would resemble Example 3:

Example 3

[code]]czoxODpcIjxmbzpwYWdlLXNlcXVlbmNlPlwiO3tbJiomXX0=[[/code] [code]]czoxMzpcIiAgICA8Zm86Zmxvdz5cIjt7WyYqJl19[[/code] [code]]czozMzpcIiAgICAgIDxmbzpibG9jaz4gPCEtLSBzZWN0aW9uIC0tPlwiO3tbJiomXX0=[[/code] [code]]czo0MjpcIiAgICAgICAgPGZvOiBibG9jaz4gPCEtLSBzZWN0aW9uIGJvZHkgLS0+IFwiO3tbJiomXX0=[[/code] [code]]czoxOTpcIiAgICAgICAgPC9mbzpibG9jaz5cIjt7WyYqJl19[[/code] [code]]czoxNzpcIiAgICAgIDwvZm86YmxvY2s+XCI7e1smKiZdfQ==[[/code] [code]]czoxNDpcIiAgICA8L2ZvOmZsb3c+XCI7e1smKiZdfQ==[[/code] [code]]czoxOTpcIjwvZm86cGFnZS1zZXF1ZW5jZT5cIjt7WyYqJl19[[/code] [code]]czoxODpcIjxmbzpwYWdlLXNlcXVlbmNlPlwiO3tbJiomXX0=[[/code] [code]]czoxMzpcIiAgICA8Zm86Zmxvdz5cIjt7WyYqJl19[[/code] [code]]czozODpcIiAgICAgIDxmbzpibG9jaz4gPCEtLSBjaGlsZCBzZWN0aW9uLS0+XCI7e1smKiZdfQ==[[/code] [code]]czo0ODpcIiAgICAgICAgPGZvOiBibG9jaz4gPCEtLSBjaGlsZCBzZWN0aW9uIGJvZHkgLS0+IFwiO3tbJiomXX0=[[/code] [code]]czoxOTpcIiAgICAgICAgPC9mbzpibG9jaz5cIjt7WyYqJl19[[/code] [code]]czoxNzpcIiAgICAgIDwvZm86YmxvY2s+XCI7e1smKiZdfQ==[[/code] [code]]czoxNDpcIiAgICA8L2ZvOmZsb3c+XCI7e1smKiZdfQ==[[/code] [code]]czoxOTpcIjwvZm86cGFnZS1zZXF1ZW5jZT5cIjt7WyYqJl19[[/code]

The block for the child section is at the same level as the block for the section, but it is in a separate page sequence. Formatting and numbering are based on the XML tree in the source document and not the output document, so they are preserved.

The FO output has been flattened into a sequence of fo:page-sequence elements. Applying this pattern to the 10,000 page document would give you hundreds or thousands of fo:page-sequence elements—and no Java memory problems.


The default XSL:FO approach

Example 4 shows XSL templates that create a single page sequence. The output from this XSL is a set of hierarchically organized fo:blocks within the single page sequence. Notice the relationship of the element to the element — one contained in the other.

These templates generate the output in Example 2, in which a single fo:page-sequence and fo:flow are wrapped around all the top-level section elements.

Example 4

[code]]czoyODpcIjx4c2w6dGVtcGxhdGUgbWF0Y2g9XCIvYm9va1wiPlwiO3tbJiomXX0=[[/code] [code]]czoyMjpcIiAgICA8Zm86cGFnZS1zZXF1ZW5jZT5cIjt7WyYqJl19[[/code] [code]]czoxNTpcIiAgICAgIDxmbzpmbG93PlwiO3tbJiomXX0=[[/code] [code]]czoxODpcIiAgICAgICAgPGZvOmJsb2NrPlwiO3tbJiomXX0=[[/code] [code]]czo0OTpcIiAgICAgICAgICA8eHNsOmFwcGx5LXRlbXBsYXRlcyBzZWxlY3Q9XCJzZWN0aW9uXCIvPlwiO3tbJiomXX0=[[/code] [code]]czoxOTpcIiAgICAgICAgPC9mbzpibG9jaz5cIjt7WyYqJl19[[/code] [code]]czoxNjpcIiAgICAgIDwvZm86Zmxvdz5cIjt7WyYqJl19[[/code] [code]]czoyMzpcIiAgICA8L2ZvOnBhZ2Utc2VxdWVuY2U+XCI7e1smKiZdfQ==[[/code] [code]]czoxNTpcIjwveHNsOnRlbXBsYXRlPlwiO3tbJiomXX0=[[/code] [code]]czozMDpcIjx4c2w6dGVtcGxhdGUgbWF0Y2g9XCJzZWN0aW9uXCI+XCI7e1smKiZdfQ==[[/code] [code]]czoxNDpcIiAgICA8Zm86YmxvY2s+XCI7e1smKiZdfQ==[[/code] [code]]czoyODpcIiAgICAgIDx4c2w6YXBwbHktdGVtcGxhdGVzLz5cIjt7WyYqJl19[[/code] [code]]czoxNTpcIiAgICA8L2ZvOmJsb2NrPlwiO3tbJiomXX0=[[/code] [code]]czoxNTpcIjwveHNsOnRlbXBsYXRlPlwiO3tbJiomXX0=[[/code] [code]]czoyNzpcIjx4c2w6dGVtcGxhdGUgbWF0Y2g9XCJib2R5XCI+XCI7e1smKiZdfQ==[[/code] [code]]czoxNDpcIiAgICA8Zm86YmxvY2s+XCI7e1smKiZdfQ==[[/code] [code]]czoyODpcIiAgICAgIDx4c2w6YXBwbHktdGVtcGxhdGVzLz5cIjt7WyYqJl19[[/code] [code]]czoxNTpcIiAgICA8L2ZvOmJsb2NrPlwiO3tbJiomXX0=[[/code] [code]]czoxNTpcIjwveHNsOnRlbXBsYXRlPlwiO3tbJiomXX0=[[/code]

The XSL:FO solution

To break the content into multiple page sequences, you modify the XSL templates to defer the creation of fo:page-sequence and fo:flow until it gets to the section element, specifically the top-level sections and the second-level sections. These functions are accomplished with two templates for sections with different modes. The solution uses a condition for selecting one template or the other based on the level of the section within the hierarchy.

The following code shows the solution. It generates the output in Example 3 (the “good” example) from the input in Example 1.

NOTE: The code shown is conceptual code. It is based on working and tested code, but has been simplified and has not itself been tested.

Example 5

[code]]czoyODpcIjx4c2w6dGVtcGxhdGUgbWF0Y2g9XCIvYm9va1wiPlwiO3tbJiomXX0=[[/code] [code]]czo1NTpcIiAgICA8eHNsOmFwcGx5LXRlbXBsYXRlcyBzZWxlY3Q9XCJzZWN0aW9uXCIgbW9kZT1cInBhZ2VcIi8+XCI7e1smKiZdfQ==[[/code] [code]]czo2MzpcIiAgICA8IS0tIFRoaXMgdGFrZXMgY2FyZSBvZiBhbGwgdGhlIHRvcC1sZXZlbCBzZWN0aW9uIHRhZ3MuIC0tPlwiO3tbJiomXX0=[[/code] [code]]czoxNTpcIjwveHNsOnRlbXBsYXRlPlwiO3tbJiomXX0=[[/code] [code]]czo4MjpcIjwhLS0gVGhlIGZvbGxvd2luZyB0ZW1wbGF0ZSBtYXRjaGVzIHNlY3Rpb24gYXQgdGhlIHRvcCBsZXZlbCBhbmQgc2Vjb257WyYqJl19ZCBsZXZlbC4gLS0+XCI7e1smKiZdfQ==[[/code] [code]]czo0MjpcIjx4c2w6dGVtcGxhdGUgbWF0Y2g9XCJzZWN0aW9uXCIgbW9kZT1cInBhZ2VcIj5cIjt7WyYqJl19[[/code] [code]]czoyMjpcIiAgICA8Zm86cGFnZS1zZXF1ZW5jZT5cIjt7WyYqJl19[[/code] [code]]czoxNTpcIiAgICAgIDxmbzpmbG93PlwiO3tbJiomXX0=[[/code] [code]]czo0NDpcIiAgICAgICAgPHhzbDphcHBseS10ZW1wbGF0ZXMgc2VsZWN0PVwiYm9keVwiLz5cIjt7WyYqJl19[[/code] [code]]czo1MTpcIiAgICAgICAgPHhzbDppZiB0ZXN0PVwiY291bnQoYW5jZXN0b3I6OnNlY3Rpb24pPTJcIi8+XCI7e1smKiZdfQ==[[/code] [code]]czo0OTpcIiAgICAgICAgICA8eHNsOmFwcGx5LXRlbXBsYXRlcyBzZWxlY3Q9XCJzZWN0aW9uXCIvPlwiO3tbJiomXX0=[[/code] [code]]czoxNzpcIiAgICAgICAgPC94c2w6aWY+XCI7e1smKiZdfQ==[[/code]

The preceding “if” condition selects third-level sections while it is processing a second-level section. You could adjust the condition to select any level of section you wanted, depending on how far down you wanted to start chunking. At this point, processing of section elements is taken over by the other “section” template. This way, the hierarchy of descendant sections is preserved within the current fo:page-sequence.

[code]]czoxNjpcIiAgICAgIDwvZm86Zmxvdz5cIjt7WyYqJl19[[/code] [code]]czoyMzpcIiAgICA8L2ZvOnBhZ2Utc2VxdWVuY2U+XCI7e1smKiZdfQ==[[/code] [code]]czo1NTpcIiAgICA8eHNsOmlmIHRlc3Q9XCJjb3VudChhbmNlc3Rvcjo6c2VjdGlvbikgJmFtcDtsdDsgMlwiPlwiO3tbJiomXX0=[[/code]

The preceding condition selects the second-level sections. You could adjust this to any level of section you wanted, depending on how far down you wanted to start the chunking. (You could also create a “chunking” variable to set at the top of this template so you could change it in only one place instead of two.)

[code]]czo1NzpcIiAgICAgIDx4c2w6YXBwbHktdGVtcGxhdGVzIHNlbGVjdD1cInNlY3Rpb25cIiBtb2RlPVwicGFnZVwiLz5cIjt7WyYqJl19[[/code]

This apply-templates invokes the current template, so the second-level sections will have the page sequence wrapped around them as well. The fo:page-sequence tag in this template is closed before this apply-templates is invoked. This is how the element hierarchy is flattened into a series of fo:page-sequences.

[code]]czoxMzpcIiAgICA8L3hzbDppZj5cIjt7WyYqJl19[[/code] [code]]czoxNTpcIjwveHNsOnRlbXBsYXRlPlwiO3tbJiomXX0=[[/code] [code]]czozMDpcIjx4c2w6dGVtcGxhdGUgbWF0Y2g9XCJzZWN0aW9uXCI+XCI7e1smKiZdfQ==[[/code] [code]]czoxNDpcIiAgICA8Zm86YmxvY2s+XCI7e1smKiZdfQ==[[/code] [code]]czoyODpcIiAgICAgIDx4c2w6YXBwbHktdGVtcGxhdGVzLz5cIjt7WyYqJl19[[/code] [code]]czoxNTpcIiAgICA8L2ZvOmJsb2NrPlwiO3tbJiomXX0=[[/code] [code]]czoxNTpcIjwveHNsOnRlbXBsYXRlPlwiO3tbJiomXX0=[[/code] [code]]czoyNzpcIjx4c2w6dGVtcGxhdGUgbWF0Y2g9XCJib2R5XCI+XCI7e1smKiZdfQ==[[/code] [code]]czoxNDpcIiAgICA8Zm86YmxvY2s+XCI7e1smKiZdfQ==[[/code] [code]]czoyODpcIiAgICAgIDx4c2w6YXBwbHktdGVtcGxhdGVzLz5cIjt7WyYqJl19[[/code] [code]]czoxNTpcIiAgICA8L2ZvOmJsb2NrPlwiO3tbJiomXX0=[[/code] [code]]czoxNTpcIjwveHNsOnRlbXBsYXRlPlwiO3tbJiomXX0=[[/code]

Two tricks worth mentioning are:

  • Wrap the fo:page-sequence tags around the elements that need to be chunked but not around the child elements that don’t need to be chunked.
  • Set up conditions to specify which elements to chunk and which not.

For the DITA OT, the solution is a little trickier. That solution is described in the following sections.

The default implementation in the DITA Open Toolkit: single page sequence

Two templates in two stylesheets handle the topic element in the DITA OT: xsl/dita2fo-shell.xsl and xsl/xslfo/topic2foImpl.xsl. Which template handles the topic element depends on whether you are using the DITA map element to organize your books.

Example 6 shows the DITA-OT template in dita-ot/xsl/dita2fo-shell.xsl that creates a single fo:page-sequence wrapping around all the topics within the map. This template is called from the dita-setup template. The context of this template is the map element. (Actually, the context is defined by match=”[contains(@class,’map/map’)]”, to be accurate.)

This style sheet operates on the temporary, merged XML file for the book in the build directory in the DITA OT, not directly on the source ditamap. In the temporary merged XML file, the map element surrounds all the topic content, not the topicrefs that it surrounds in the ditamap files. So even though the context is the map element, when this template issues the element, the element it works on is a topic (or an element related to a topic).

Notice, as you did in Example 4, the relationship between the xsl:apply-template element for topics and the fo:page-sequence element. One is contained inside the other.

Example 6

[code]]czozMTpcIjx4c2w6dGVtcGxhdGUgbmFtZT1cIm1haW4tZG9jM1wiPlwiO3tbJiomXX0=[[/code] [code]]czo1NjpcIiAgICA8Zm86cGFnZS1zZXF1ZW5jZSBtYXN0ZXItcmVmZXJlbmNlPVwiY2hhcHRlci1tYXN0ZXJcIj5cIjt7WyYqJl19[[/code] [code]]czo1NTpcIiAgICAgIDxmbzpzdGF0aWMtY29udGVudCBmbG93LW5hbWU9XCJ4c2wtcmVnaW9uLWJlZm9yZVwiPlwiO3tbJiomXX0=[[/code] [code]]czoxMTpcIiAgICAgICAgLi4uXCI7e1smKiZdfQ==[[/code] [code]]czoyNjpcIiAgICAgIDwvZm86c3RhdGljLWNvbnRlbnQ+XCI7e1smKiZdfQ==[[/code] [code]]czo1NDpcIiAgICAgIDxmbzpzdGF0aWMtY29udGVudCBmbG93LW5hbWU9XCJ4c2wtcmVnaW9uLWFmdGVyXCI+XCI7e1smKiZdfQ==[[/code] [code]]czoxMTpcIiAgICAgICAgLi4uXCI7e1smKiZdfQ==[[/code] [code]]czoyNjpcIiAgICAgIDwvZm86c3RhdGljIGNvbnRlbnQ+XCI7e1smKiZdfQ==[[/code] [code]]czo0MzpcIiAgICAgIDxmbzpmbG93IGZsb3ctbmFtZT1cInhzbC1yZWdpb24tYm9keVwiPlwiO3tbJiomXX0=[[/code] [code]]czo5NzpcIiAgICAgICAgPGZvOmJsb2NrIHRleHQtYWxpZ249XCJsZWZ0XCIgZm9udC1zaXplPVwiMTBwdFwiIGZvbnQtZmFtaWx5PVwiSGVsdntbJiomXX1ldGljYVwiIGJyZWFrLWJlZm9yZT1cInBhZ2VcIj5cIjt7WyYqJl19[[/code] [code]]czozMjpcIiAgICAgICAgICA8eHNsOmFwcGx5LXRlbXBsYXRlcy8+XCI7e1smKiZdfQ==[[/code] [code]]czoxOTpcIiAgICAgICAgPC9mbzpibG9jaz5cIjt7WyYqJl19[[/code] [code]]czoxNjpcIiAgICAgIDwvZm86Zmxvdz5cIjt7WyYqJl19[[/code] [code]]czoyMzpcIiAgICA8L2ZvOnBhZ2Utc2VxdWVuY2U+XCI7e1smKiZdfQ==[[/code] [code]]czoxNDpcIjx4c2w6dGVtcGxhdGU+XCI7e1smKiZdfQ==[[/code]

Example 7 shows the DITA-OT template in xsl/xslfo/topic2foImpl.xsl that matches a top-level topic when there is no ditamap:

Example 7

[code]]czo4ODpcIjx4c2w6dGVtcGxhdGUgbWF0Y2g9XCIqW2NvbnRhaW5zKEBjbGFzcyxcJ3RvcGljL3RvcGljXCcpXVwiIG5hbWU9XCJ0b3B0b3BpY3tbJiomXX1cIiBtb2RlPVwidG9wbGV2ZWxcIj5cIjt7WyYqJl19[[/code] [code]]czo0NTpcIiAgICA8eHNsOmNhbGwtdGVtcGxhdGUgbmFtZT1cImNoYXB0ZXItc2V0dXBcIi8+XCI7e1smKiZdfQ==[[/code] [code]]czoxNTpcIjwveHNsOnRlbXBsYXRlPlwiO3tbJiomXX0=[[/code]

The following template matches all other topics, whether they are embedded in a map or a top-level topic. This template only creates fo:block elements, not fo:page-sequence elements.

[code]]czo1NTpcIjx4c2w6dGVtcGxhdGUgbWF0Y2g9XCIqY29udGFpbnMoQGNsYXNzLFwndG9waWMvdG9waWNcJyldXCI+XCI7e1smKiZdfQ==[[/code] [code]]czoxNDpcIiAgICA8Zm86YmxvY2s+XCI7e1smKiZdfQ==[[/code] [code]]czoyODpcIiAgICAgIDx4c2w6YXBwbHktdGVtcGxhdGVzLz5cIjt7WyYqJl19[[/code] [code]]czoxNTpcIiAgICA8L2ZvOmJsb2NrPlwiO3tbJiomXX0=[[/code] [code]]czoxNTpcIjwveHNsOnRlbXBsYXRlPlwiO3tbJiomXX0=[[/code]

The following template provides the fo:root and fo:page-sequence elements when the top-level topics are not contained in a map element. Just like the structure in , the xsl:apply-template element for lower-level topics is contained inside the fo:page-sequence element.

[code]]czozNTpcIjx4c2w6dGVtcGxhdGUgbmFtZT1cImNoYXB0ZXItc2V0dXBcIj5cIjt7WyYqJl19[[/code] [code]]czo2MjpcIiAgICA8IS0tTmV3bGluZSBjaGFyYWN0ZXIgKGNhcHR1cmUgdGhlIG5hdGl2ZSBmaWxlIG5ld2xpbmUpLS0+XCI7e1smKiZdfQ==[[/code] [code]]czozNDpcIiAgICA8eHNsOnZhcmlhYmxlIG5hbWU9XCJuZXdsaW5lXCIvPlwiO3tbJiomXX0=[[/code] [code]]czo1ODpcIiAgICA8Zm86cm9vdCB4bWxuczpmbz1cImh0dHA6Ly93d3cudzMub3JnLzE5OTkvWFNML0Zvcm1hdFwiPlwiO3tbJiomXX0=[[/code] [code]]czoyODpcIiAgICAgIDxmbzpsYXlvdXQtbWFzdGVyLXNldD5cIjt7WyYqJl19[[/code] [code]]czoxMTpcIiAgICAgICAgLi4uXCI7e1smKiZdfQ==[[/code] [code]]czoyOTpcIiAgICAgIDwvZm86bGF5b3V0LW1hc3RlciBzZXQ+XCI7e1smKiZdfQ==[[/code] [code]]czo4MzpcIiAgICAgIDxmbzpwYWdlLXNlcXVlbmNlIG1hc3Rlci1yZWZlcmVuY2U9XCJjb21tb24tcGFnZVwiIGZvcmNlLXBhZ2UtY291bntbJiomXX10PVwibm8tZm9yY2VcIj5cIjt7WyYqJl19[[/code] [code]]czo5NzpcIiAgICAgICAgPGZvOnN0YXRpYy1jb250ZW50IGZsb3ctbmFtZT1cInhzbC1yZWdpb24tYmVmb3JlXCIgZm9udC1zaXplPVwiOXB7WyYqJl19dFwiIGZvbnQtZmFtaWx5PVwiSGVsdmV0aWNhXCI+XCI7e1smKiZdfQ==[[/code] [code]]czoxMzpcIiAgICAgICAgICAuLi5cIjt7WyYqJl19[[/code] [code]]czoyODpcIiAgICAgICAgPC9mbzpzdGF0aWMtY29udGVudD5cIjt7WyYqJl19[[/code] [code]]czo1NjpcIiAgICAgICAgPGZvOnN0YXRpYy1jb250ZW50IGZsb3ctbmFtZT1cInhzbC1yZWdpb24tYWZ0ZXJcIj5cIjt7WyYqJl19[[/code] [code]]czoxMzpcIiAgICAgICAgICAuLi5cIjt7WyYqJl19[[/code] [code]]czoyODpcIiAgICAgICAgPC9mbzpzdGF0aWMtY29udGVudD5cIjt7WyYqJl19[[/code] [code]]czoxNzpcIiAgICA8IS0tIGJvZHkgLS0+XCI7e1smKiZdfQ==[[/code] [code]]czo0NTpcIiAgICAgICAgPGZvOmZsb3cgZmxvdy1uYW1lLVwieHNsLXJlZ2lvbi1ib2R5XCI+XCI7e1smKiZdfQ==[[/code] [code]]czo5MjpcIiAgICAgICAgICA8Zm86YmxvY2sgbGluZS1oZWlnaHQ9XCIxMHB0XCIgZm9udCBzaXplPVwiOXB0XCIgZm9udC1mYW1pbHk9XCJIZXtbJiomXX1sdmV0aWNhXCIgaWQ9XCJwYWdlMS0xXCI+XCI7e1smKiZdfQ==[[/code] [code]]czozNDpcIiAgICAgICAgICAgIDx4c2w6YXBwbHktdGVtcGxhdGVzLz5cIjt7WyYqJl19[[/code] [code]]czoyMTpcIiAgICAgICAgICA8L2ZvOmJsb2NrPlwiO3tbJiomXX0=[[/code] [code]]czoxODpcIiAgICAgICAgPC9mbzpmbG93PlwiO3tbJiomXX0=[[/code] [code]]czoyNTpcIiAgICAgIDwvZm86cGFnZS1zZXF1ZW5jZT5cIjt7WyYqJl19[[/code] [code]]czoxMzpcIiAgICA8L2ZvOnJvb3RcIjt7WyYqJl19[[/code] [code]]czoxNTpcIjwveHNsOnRlbXBsYXRlPlwiO3tbJiomXX0=[[/code]

It is important to remember that in the DITA-OT, DITA files can be processed in two ways: as stand-alone DITA documents or as ditamaps. The dita2fo-shell.xsl file sets up the page flow when a ditamap is being processed. If a single DITA file is processed, the document is structured with the top element having a class attribute that contains the string topic/topic. The top-level topic is processed with the template:

[code]]czo4ODpcIjx4c2w6dGVtcGxhdGUgbWF0Y2g9XCIqW2NvbnRhaW5zKEBjbGFzcyxcJ3RvcGljL3RvcGljXCcpXVwiIG5hbWU9XCJ0b3B0b3BpY3tbJiomXX1cIiBtb2RlPVwidG9wbGV2ZWxcIj5cIjt7WyYqJl19[[/code]

which in turn calls the template:

[code]]czozNjpcIjx4c2w6dGVtcGxhdGUgbmFtZT1cImNoYXB0ZXItc2V0dXBcIj4gXCI7e1smKiZdfQ==[[/code]

For both processing as a stand-alone DITA document or a ditamap, topics beneath the map element or the top-level topic are processed by the template that begins:

[code]]czo1NTpcIjx4c2w6dGVtcGxhdGUgbWF0Y2g9XCIqY29udGFpbnMoQGNsYXNzLFwndG9waWMvdG9waWNcJyldXCI+XCI7e1smKiZdfQ==[[/code]

But by this time, the fo:page-sequence has already been created, so the whole book is now in a single large fo:page-sequence. This is the root cause of the memory problem for large books.

The solution in the DITA Open Toolkit: multiple page sequences

Modifying the transformation process to create multiple page sequences and preventing them from wrapping each other eliminates the memory problem. Example 8 and Example 9 show the solution. The altered code processes all topics and subtopics inside their own page sequences. Therefore, the map template does not start any page sequence at all. Each topic template (including the top-level topic template) creates a page sequence and also places the for child topics outside the fo:page-sequence element, so fo:page-sequences are not nested.

Example 8 shows the changes in dita-ot/xsl/dita2fo-shell.xsl.

NOTE: Bold type indicates where code is added to or deleted from the original templates.

Example 8

[code]]czozMTpcIjx4c2w6dGVtcGxhdGUgbmFtZT1cIm1haW4tZG9jM1wiPlwiO3tbJiomXX0=[[/code] [code]]czo1OlwiICAgICBcIjt7WyYqJl19[[/code]<![code]]czoyOlwiLS1cIjt7WyYqJl19[[/code]deleted all the page sequence stuff.[code]]czozOlwiIC0tXCI7e1smKiZdfQ==[[/code]>
[code]]czo0ODpcIiAgICAgPHhzbDphcHBseS10ZW1wbGF0ZXM+PC94c2w6YXBwbHktdGVtcGxhdGVzPlwiO3tbJiomXX0=[[/code] [code]]czoxNTpcIjwveHNsOnRlbXBsYXRlPlwiO3tbJiomXX0=[[/code]

Example 9 shows the modified templates in dita-ot/xsl/topic2foImpl.xsl.

Example 9

[code]]czo5ODpcIjx4c2w6dGVtcGxhdGUgbWF0Y2g9XCIqW2NvbnRhaW5zKEBjb250YWlucyhAY2xhc3MsXCd0b3BpYy90b3BpY1wnKV1cIiBuYW1le1smKiZdfT1cInRvcHRvcGljXCIgbW9kZT1cInRvcGxldmVsXCI+XCI7e1smKiZdfQ==[[/code] [code]]czo0NTpcIiAgICA8eHNsOmNhbGwtdGVtcGxhdGUgbmFtZT1cImNoYXB0ZXItc2V0dXBcIi8+XCI7e1smKiZdfQ==[[/code] [code]]czoxNTpcIjwveHNsOnRlbXBsYXRlPlwiO3tbJiomXX0=[[/code] [code]]czo1NjpcIjx4c2w6dGVtcGxhdGUgbWF0Y2g9XCIqW2NvbnRhaW5zKEBjbGFzcyxcJ3RvcGljL3RvcGljXCcpXVwiPlwiO3tbJiomXX0=[[/code] [code]]czo1OlwiICAgICBcIjt7WyYqJl19[[/code]<fo:page-sequence master-reference=”chapter-master”>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czo5OlwiICAgICAgLi4uXCI7e1smKiZdfQ==[[/code] [code]]czo3OlwiICAgICAgIFwiO3tbJiomXX0=[[/code]<fo:static-content flow-name=”xsl-region-before”>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czo2OlwiICAgICAgXCI7e1smKiZdfQ==[[/code]</fo:static-content>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czo2OlwiICAgICAgXCI7e1smKiZdfQ==[[/code]<fo:static-content flow-name=”xsl-region-after”>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czo3OlwiICAgICAgIFwiO3tbJiomXX0=[[/code]</fo:static-content>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czo3OlwiICAgICAgIFwiO3tbJiomXX0=[[/code]<fo:flow flow-name=”xsl-region-body”>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code]

Now, instead of a generic apply-templates, use select attributes to process child elements in the same order they occur in DITA. However, place the apply-templates that selects child topics outside the fo:page-sequence tags.

[code]]czoxODpcIiAgICAgICAgPGZvOmJsb2NrPlwiO3tbJiomXX0=[[/code] [code]]czowOlwiXCI7e1smKiZdfQ==[[/code]<![code]]czozOlwiLS0gXCI7e1smKiZdfQ==[[/code]The generic apply-templates is replaced with specific apply-templates[code]]czozOlwiIC0tXCI7e1smKiZdfQ==[[/code]>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czoxMTpcIiAgICAgICAgICAgXCI7e1smKiZdfQ==[[/code]<xsl:apply-templates select=”*[contains(@class,’topic/title’)]”/>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czoxMTpcIiAgICAgICAgICAgXCI7e1smKiZdfQ==[[/code]<xsl:apply-templates select=”*[contains(@class,’topic/titlealts’)]”/>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czoxMTpcIiAgICAgICAgICAgXCI7e1smKiZdfQ==[[/code]<xsl:apply-templates select=”*[contains(@class,’topic/shortdesc’)]”/>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czoxMTpcIiAgICAgICAgICAgXCI7e1smKiZdfQ==[[/code]<xsl:apply-templates select=”*[contains(@class,’topic/prolog’)]”/>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czoxMTpcIiAgICAgICAgICAgXCI7e1smKiZdfQ==[[/code]<xsl:apply-templates select=”*[contains(@class,’topic/body’)]”/>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czoxMTpcIiAgICAgICAgICAgXCI7e1smKiZdfQ==[[/code]<xsl:apply-templates select=”*[contains(@class,’topic/related-links’)]”/>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czoxOTpcIiAgICAgICAgPC9mbzpibG9jaz5cIjt7WyYqJl19[[/code] [code]]czo3OlwiICAgICAgIFwiO3tbJiomXX0=[[/code]</fo:flow>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czo1OlwiICAgICBcIjt7WyYqJl19[[/code]</fo:page-sequence>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czowOlwiXCI7e1smKiZdfQ==[[/code]<!– The apply-templates for child topics is invoked outside the page sequence. –>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czo1OlwiICAgICBcIjt7WyYqJl19[[/code]<xsl:apply-templates select=”*[contains(@class,’topic/topic’)]”/>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czoxNTpcIjwveHNsOnRlbXBsYXRlPlwiO3tbJiomXX0=[[/code] [code]]czozNTpcIjx4c2w6dGVtcGxhdGUgbmFtZT1cImNoYXB0ZXItc2V0dXBcIj5cIjt7WyYqJl19[[/code] [code]]czo1ODpcIjwhLS1OZXdsaW5lIGNoYXJhY3RlciAoY2FwdHVyZSB0aGUgbmF0aXZlIGZpbGUgbmV3bGluZSktLT5cIjt7WyYqJl19[[/code] [code]]czozNDpcIiAgICA8eHNsOnZhcmlhYmxlIG5hbWU9XCJuZXdsaW5lXCIvPlwiO3tbJiomXX0=[[/code] [code]]czo1ODpcIiAgICA8Zm86cm9vdCB4bWxuczpmbz1cImh0dHA6Ly93d3cudzMub3JnLzE5OTkvWFNML0Zvcm1hdFwiPlwiO3tbJiomXX0=[[/code] [code]]czoyODpcIiAgICAgIDxmbzpsYXlvdXQtbWFzdGVyLXNldD5cIjt7WyYqJl19[[/code] [code]]czoxMTpcIiAgICAgICAgLi4uXCI7e1smKiZdfQ==[[/code] [code]]czoyOTpcIiAgICAgIDwvZm86bGF5b3V0LW1hc3RlciBzZXQ+XCI7e1smKiZdfQ==[[/code] [code]]czo4MzpcIiAgICAgIDxmbzpwYWdlLXNlcXVlbmNlIG1hc3Rlci1yZWZlcmVuY2U9XCJjb21tb24tcGFnZVwiIGZvcmNlLXBhZ2UtY291bntbJiomXX10PVwibm8tZm9yY2VcIj5cIjt7WyYqJl19[[/code] [code]]czo5NzpcIiAgICAgICAgPGZvOnN0YXRpYy1jb250ZW50IGZsb3ctbmFtZT1cInhzbC1yZWdpb24tYmVmb3JlXCIgZm9udC1zaXplPVwiOXB7WyYqJl19dFwiIGZvbnQtZmFtaWx5PVwiSGVsdmV0aWNhXCI+XCI7e1smKiZdfQ==[[/code] [code]]czoxMzpcIiAgICAgICAgICAuLi5cIjt7WyYqJl19[[/code] [code]]czoyODpcIiAgICAgICAgPC9mbzpzdGF0aWMtY29udGVudD5cIjt7WyYqJl19[[/code] [code]]czo1NjpcIiAgICAgICAgPGZvOnN0YXRpYy1jb250ZW50IGZsb3ctbmFtZT1cInhzbC1yZWdpb24tYWZ0ZXJcIj5cIjt7WyYqJl19[[/code] [code]]czoxMzpcIiAgICAgICAgICAuLi5cIjt7WyYqJl19[[/code] [code]]czoyODpcIiAgICAgICAgPC9mbzpzdGF0aWMtY29udGVudD5cIjt7WyYqJl19[[/code] [code]]czoxNzpcIiAgICA8IS0tIGJvZHkgLS0+XCI7e1smKiZdfQ==[[/code] [code]]czo0NTpcIiAgICAgICAgPGZvOmZsb3cgZmxvdy1uYW1lLVwieHNsLXJlZ2lvbi1ib2R5XCI+XCI7e1smKiZdfQ==[[/code]

Again, instead of a generic apply-templates, use select attributes to process child elements in the same order they occur in DITA. Place the apply-templates that select children topics outside the page sequence tags.

[code]]czoxMTpcIiAgICAgICAgICAgXCI7e1smKiZdfQ==[[/code]<xsl:apply-templates select=”*[contains(@class,’topic/title’)]”/>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czoxMTpcIiAgICAgICAgICAgXCI7e1smKiZdfQ==[[/code]<xsl:apply-templates select=”*[contains(@class,’topic/titlealts’)]”/>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czoxMTpcIiAgICAgICAgICAgXCI7e1smKiZdfQ==[[/code]<xsl:apply-templates select=”*[contains(@class,’topic/shortdesc’)]”/>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czoxMTpcIiAgICAgICAgICAgXCI7e1smKiZdfQ==[[/code]<xsl:apply-templates select=”*[contains(@class,’topic/prolog’)]”/>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czoxMTpcIiAgICAgICAgICAgXCI7e1smKiZdfQ==[[/code]<xsl:apply-templates select=”*[contains(@class,’topic/body’)]”/>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czoxMTpcIiAgICAgICAgICAgXCI7e1smKiZdfQ==[[/code]<xsl:apply-templates select=”*[contains(@class,’topic/related-links’)]”/>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czoxODpcIiAgICAgICAgPC9mbzpmbG93PlwiO3tbJiomXX0=[[/code] [code]]czoyNTpcIiAgICAgIDwvZm86cGFnZS1zZXF1ZW5jZT5cIjt7WyYqJl19[[/code] [code]]czo3OlwiICAgICAgIFwiO3tbJiomXX0=[[/code]<xsl:apply-templates select=”*[contains(@class,’topic/topic’)]”/>>[code]]czowOlwiXCI7e1smKiZdfQ==[[/code] [code]]czoxNDpcIiAgICA8L2ZvOnJvb3Q+XCI7e1smKiZdfQ==[[/code] [code]]czoxNTpcIjwveHNsOnRlbXBsYXRlPlwiO3tbJiomXX0=[[/code]

This solution puts every topic in its own page sequence. This approach is adjustable and can separate topics into individual page sequences down to a specified level in the hierarchy, as described in The default XSL:FO approach.

Conclusion

XSL:FO is a great way to create PDF files from XML, but very large PDF files present memory issues. The cause is frequently the use of a single fo:page-sequence to contain the entire body of a book. Chunking the book into multiple fo:page-sequences can prevent these memory problems. The XSL:FO needs to generate page sequences with each child element parallel to the parent’s page sequences—not embedded in them.

This approach is especially useful for DITA because it embeds the same XML element (“topic”) at many levels. The strategy outlined in this document has the flexibility to chunk topics down to a specified level. Using this approach, forced page breaks are kept to a minimum while fo:page-sequence sections are adjusted for the memory limitations of the FO processor.

About the Author

ScriptoriumTech

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.