Recursive processing

Typically, it is necessary to process all elements in a document, starting with the root element, then working through its children, but processing the descendants of each child before proceeding to the next (in the same manner as shown for linear processing of a tree structure, as discussed in Chapter 16). In XSLT, this does not happen automatically.

Applying templates

The Apply Templates element must be used to indicate that children of the current element are to be processed. In the example below, the Emphasis element rule is only triggered if the Paragraph rule contains this instruction, and the text within the Emphasis element is only processed (presented) if the same instruction is included:

   <para>An <emph>emphasized</emph> word.</para>


<template match="para">
  <apply-templates/>
</template>

<template match="emph">
  <apply-templates/>
</template>

Default template

However, this is not sufficient on its own. Normally, a stylesheet author would not bother to create templates for elements that do not need to be formatted or translated differently to their ancestor elements. Yet some of these 'ignored' elements may themselves contain further elements that do require such attention. A rule is therefore needed to act as a 'catch-all', representing the elements not covered by explicit formatting rules.

All XSLT processors should include an implied rule that acts as if the following explicit rule were present. Following XPath standard conventions, this rule uses the wildcard operator, '*', to represent all elements, and the '/' symbol to represent the root of the document. This rule is less important than others (see below for importance rules), so will only be activated for elements that are not dealt with by explicit templates:

<template match="/|*">
  <apply-templates/>
</template>

Although this is an implied rule, it can also be included explicitly, so allowing its default action to be modified. For example, the Apply Templates element could be removed, in which case the content of elements that do not have an associated rule will not be processed.

Text processing default

Similarly, a built-in rule is needed to present the text content of every element. This rule uses the ' text()' function to achieve this (the content of this template is explained later):

<template match="text()">
  <value-of select="."/>
</template>

PIs and comments

Note that comments and processing instructions are by default not processed. Explicit rules similar to the one above, but using the 'processing-instruction()' or 'comment()' functions, must be included to process these items.

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

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