Applying Templates with xsl:apply-templates
By default, an XSLT processor reads the input XML document from top to bottom, starting at the root of the document and working its way down using preorder traversal. Template rules are activated in the order in which they match elements encountered during this traversal. This means a template rule for a parent will be activated before template rules matching the parent’s children.
However, one of the things a template can do is change the order of traversal. That
is, it can specify which element(s) should be processed next. It can
specify that an element(s) should be processed in the middle of
processing another element. It can even prevent particular elements
from being processed. In fact, Examples Example 8-4 through Example 8-6 all implicitly prevent
the child elements of each person
element from being processed. Instead, they provided their own
instructions about what the XSLT processor was and was not to do with
those children.
The xsl:apply-templates
element makes the processing order explicit. Its select
attribute contains an XPath expression telling the XSLT processor which nodes
to process at that point in the output tree.
For example, suppose you wanted to list the names of the people in the input document; however, you want to put the last names first, regardless of the order in which they occur in the input document, and you don’t want to output the professions or hobbies. First you need a name template that looks like this: ...
Get XML in a Nutshell, 3rd 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.