Patterns

A pattern is a limited form of location path (described next). Patterns are used in XSLT when the need is to decide whether the current element appears in a given context. Patterns are only allowed to contain some expression constructs. As the need for patterns is limited to matching XSLT templates, the definition of patterns is actually provided in the XSLT standard, not the XPath standard.

Where patterns are allowed

Patterns are used in the Match attribute within the Key and Template elements, as well as the Count and From attributes within the Number element:

<template match="pattern">...</template>


<key match="pattern">...</key>


<number count="pattern" from="pattern">...</number>

Matching elements to patterns

Unlike other expressions, patterns are read from right to left. Consider the following Match attribute in the Template element:

In this example, the first part of the pattern (part (1)) is compared against the current node. If the current node is a Para element, as required, then the next part of the pattern (part (2)) is compared against the parent of the current element. Providing that the parent is a Warning element, the final check is that this element is in turn enclosed by a Book element. In this example, the template is a candidate for the current element provided that the pattern accurately describes the current context.

Multiple patterns

A pattern can be divided into several distinct sub-patterns, separated by the vertical bar symbol, '|'. If any of the sub-patterns is valid, the entire pattern succeeds. For example, the following template matches Note, Warning and Danger elements. If the current element is any of these, then the template is triggered:

<template match="note|warning|danger">...</template>


   <para>...</para>
   <note>...</note>
   <para>...</para>
   <danger>...</danger>
						<warning>...</warning>
					

Steps

Many patterns consist of a number of steps. Each step is separated from other steps using '/'. The following example has two steps, and it matches paragraphs within chapters:

match="chapter/para"


   <chapter>
     <para>This is a chapter paragraph.</para>
						<para>This is another chapter paragraph.</para>
       <section>
         <para>This is a section paragraph.</para>

When a step is empty, there are two adjacent '/' symbols, '//', indicating any number of intermediate elements of any kind. This example matches all of the following paragaphs:

match="book//para"


   <book>
     <para>This is a book.</para>
     <chapter>
       <para>This is a chapter paragraph.</para>
       <section>
         <para>This is a section paragraph.</para>
					

A pattern must begin with an element name, a document root identifier, '/', a root and unknown-number-of-descendants identifier, '//', or an element identifier keyword, 'id()' or 'key()':

match="/..."

match="//..."

match="id('link99')/..."

match="key('color', 'yellow')/..."

Each step in the pattern is usually an element name or an attribute name (prefixed with '@'), or an element or attribute name prefixed with the keywords 'child::' or 'attribute::':

match=".../chapter/..."

match=".../child::chapter/..."

match=".../@author"

match=".../attribute::author"

The symbol '*' represents any element or any attribute, depending on whether or not it is preceded by '@' ('@*'):

match=".../*/..."

match=".../@*"

However, other node types can be included, and element names may have a namespace prefix.

match=".../*/..."

match=".../acme:price/..."

match=".../comment()"

match=".../text()"

match=".../processing-instruction()"

match=".../node()/..."

But most of these are only relevant as the final step in the pattern, as they cannot include children. For example, it does not make sense to ask whether a Para element is within a comment (because XML rules do not allow this).

In addition, one or more predicate filters are also allowed (see below):

match=".../chapter[@author='J Smith']/..."

It is not possible for a pattern to include relative paths, including '.' and '..' instructions. A pattern must also not include direction keywords beyond 'child::' and 'attribute::'. For example, 'ancestor::' may not be used. This is because a pattern is used to 'zero-in' on the current node from the document root or an identified element. It does not make sense to allow the path to 'meander' around the document structure.

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

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