Sorting elements

When the source document contains a list of items that are not arranged in any order, but need to be output in alphabetical or numerical order, it is possible to sort these items. The Sort element is used within the Apply Templates element to sort the elements it selects:

<list>
  <item>Zimbabwe</item>
  <item>Morocco</item>
  <item>Zaire</item>
  <item>Algeria</item>
</list>
<template match="list">
  ... <apply-templates><sort/></apply-templates> ...
</template>

   Algeria   Morocco   Zaire   Zimbabwe

Note that, contrary to the impression given by earlier examples, the Apply Templates element is not always an empty element, and other scenarios covered later also require this element to be present as a container element.

Basic sorting ...

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.