Selective processing

Using the Apply Templates element as described above, it is a rather blunt instrument that performs a useful but simple task. However, it can be made much more flexible. It can be used to select specific elements to be processed, not just descendents of the current element.

Specific children

The Apply Templates element can take a Select attribute, which overrides the default action of processing all children. Using XPath patterns, it is possible to select specific children and to ignore others. In the following example, the rule that matches a list of names targets only the embedded Name elements with a Type attribute value of 'company':

<template match="names">
  <apply-templates select="name[@type='company']"/> </template> ...

Get XML Companion, The, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.