Information reuse

There are a number of reasons why information may need to be copied to another location in the output document. One typical example is the need to create a table of contents for a book. In this case, the chapter titles need to be copied to the beginning of the document.

Previous examples have shown how the Apply Templates element is normally used to select the children of the current element, so that the entire document can be processed in the correct order (in a tree-walking manner). However, it is also able to select material from elsewhere in the document, using the Select attribute. It has already been shown that this attribute can be used to select specific children of the current element, but as it can hold any XPath expression, it can also be used to select material from other locations. In the following example, instead of children of the current element, the chapter titles are selected:

<apply-templates select="/book/chapter/title" />

In this example, each chapter is selected in turn, and each title in these chapters is sought. Templates are applied to each matching Title element, and the output from these templates appears at the current location (whereby a table of contents is created).

Note that the ability to trigger templates to process material beyond the scope of the current element is potentially dangerous. It is possible to select the parent, or indeed any ancestor of the current element. This can lead to a processing loop that never ends. Some XSLT processors may detect and escape from this condition, but others may not.

It should be recalled that a template can hold more than one Apply Templates element. A single template may process the children of the current element, as well as grabbing material from elsewhere. The following template processes the content of a book, and also 'drags up' the titles from each chapter to build the table of contents:

<xsl:template match="book">
  <HTML>
    <HEAD><TITLE>A book</TITLE></HEAD>
    <BODY>
      <DIV>
        <xsl:apply-templates select="chapter/title" />
      </DIV>
      <xsl:apply-templates/>
    </BODY>
  </HTML>
</xsl:template>

When the structure and element names in the information to be copied does not alter on output, the Copy Of element can be used instead. The following example copies the Title elements in chapters directly to output (and does not change the names of the Title element, or any of the other elements):

   <book>
     <chapter>
       <title>title one</title>
       ...
     </chapter>

     <chapter>
       <title>title <emph>two</emph></title>
       ...
     </chapter>
   </book>


<xsl:template match="book">
  <out-book>
    <table-of-contents>
      <xsl:copy-of select="/chapter/title" />
    </table-of-contents>
    <xsl:apply-templates />
  </out-book>
</xsl:template>
   <out-book>
     <table-of-contents>
       <title>title one</title>
					<title>title <emph>two</emph></title>
     </table-of-contents>
     ...
   </out-book>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.33.207