#### Splitting documents into chunks

Sometimes we want to generate multiple output documents from a single XML source. It may for example be a bad idea to transform a book of 200 printed pages into a single online HTML page. Instead we may split each chapter into a separate HTML file and create navigation links between them.

We consider a memo document instance. We want to generate one text file for each memo recipient containing just the recipient's name using the XSL element <xsl:result-document>:

<xsl:template match="/memo">
<xsl:apply-templates select="to"/>
</xsl:template>

<xsl:template match="to">
<xsl:result-document
❶
href="file_{position()}.txt"
❷
method="text"
❸>
<xsl:value-of select="."/> ❹

</xsl:result-document>
</xsl:template>
 The output from all generating XSL directives will be redirected from standard output to another output channel. The output will be written to a file named file_i.txt with decimal number i ranging from value 1 up to the number of recipients. The method attribute possibly overrides a value being given in the  element. We may also redefine other attributes from  like doctype-{public.system} and the generated file's encoding. All output being generated in this region gets redirected to the channel specified in ❷.

No. 58

##### Splitting book into chapter files
 Q: Extend your solution of Extending the memo style sheet by mixed content and itemized lists by writing each 's content into a separate Xhtml file. In addition create a file index.html which contains references to the corresponding  documents. Thus for a document instance with two chapters the overall navigation structure is illustrated by Figure 964, “A  document with two chapters ”. Implementing the  tag may cause a problem: An internal link may reference a . You need to identify the  node embedding this para. This may be done by using a suitable XPath axis direction. A: The full source code of the solution is available at (Online HTML version) ... book2chunks.1.xsl. First we generate the table of contents file index.html:  <xsl:value-of select="title"/>

Table of contents

The  may reference a  or a . So we may need to “step up” from a paragraph to the corresponding chapter node:  This is consistent since all 

nodes in the generated Xhtml receive a unique id value regardless whether the originating  node does have one.