Priorities

Only one template can ever be selected by an XSLT processor to format a specific instance of an element. In the expressions shown in previous chapters, there is one template for each element type, but the introduction of context-specific templates means that a number of different templates may be applicable. The following two templates both apply to a Para element. The first template matches paragraphs within the introduction of a book. The second matches paragraphs within a Warning element. It can be imagined that, in some circumstances, there will be a warning within an introduction. In this case, both templates would be applicable:

<template match="introduction//para">...</template>
<template match="warning//para">...</template>

When this possibility occurs, it is necessary to specify which of the candidate templates is to be chosen. This is done using the Priority attribute:

<template ... priority="2">...</template>

Priority values

One template is given precedence over another by assigning to it a higher priority value. In the following example, the warning paragraph takes priority over the introduction paragraph. The paragraph will therefore look the same in all warnings, regardless of whether or not they are also in the introduction section of the document:

<template match="introduction//para" priority="1">...

<template match="warning//para" priority="2">...

It does not matter where these two templates appear within the stylesheet, or which appears first.

Default values

It is not always necessary to use the Priority attribute. When it is not included, a default priority value is assumed. But the default value varies, depending on the content of the Match attribute.

Simple expressions that refer only to an element name, qualified with a namespace prefix, whether it includes the 'child::' prefix or not, have a default value of '0':

<!-- PRIORITY '0' -->
<template match="x:para">...</template>

<!-- PRIORITY '0' -->
<template match="child::x:para">...</template>

Other defaults are even less specific, so have values below zero.

An element name with no prefix has a default value of '-0.25'. This means that qualified names are more specific than unqualified names:

<!-- PRIORITY '0' -->
<template match="x:para">...</template>

<!-- PRIORITY '-0.25' -->
<template match="para">...</template>

Finally, the least specific form of all is a simple node test:

<!-- PRIORITY '-0.5' -->

<template match="*">...</template>
<template match="comment()">...</template>

<template match="text()">...</template>

<template match="processing-instruction()">...</template>

<template match="node()">...</template>

There is therefore a distinction between any element ('*'), and any element with a specific namespace qualifier ('abc:*'), which overrides the more general case:

<!-- LOWER PRIORITY (-0.5) -->
<template match="*">...</template>

<!-- HIGHER PRIORITY (-0.25) -->
<template match="abc:*">...</template>

All other cases default to a value of '0.5', and so a contextual template automatically overrides a simple element-matching template. It is not necessary to introduce the Priority attribute to distinguish between the general and special case in the example below:

<!-- LOWER PRIORITY (0) -->
<template match="para">...</template>

<!-- HIGHER PRIORITY (0.5) -->
<template match="warning/para">...</template>

However, when there is more than one contextual match, or when some contexts are more important than others, the Priority attribute is needed, and values should start from '1' to ensure that the default cases are overridden.

Conflict resolution

When two or more templates remain applicable (regardless of whether or not the Priority attribute has been used in some or all templates), this may be considered to be an error by the XSLT processor. The processor may report the problem and cease to function; but most will recover and, if they follow the standard, will simply select the template closest to the end of the stylesheet. If a processor that behaves in this latter way is chosen, then the order in which templates are placed in the file becomes a very important stylesheet design consideration:

<template match="book//para">...</template>

<!-- MORE SPECIFIC - APPEARS LAST -->
<template match="warning//para">...</template>

Finally, it would be a mistake to think that the length of an expression has any effect on default priority values. The two examples below both default to '0.5':

<template match="warning//para">...</template>

<template match="book/warning/para[@type='secret']">...

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

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