Grouping Using group-ending-with

The last grouping style is group-ending-with. This is similar to group-starting-with, only we’re specifying the condition that ends each group. We’ll use this technique to insert items into a three-column HTML table. Each row in the table is created from a group; each group will have no more than three members. We handle the special case of a row with less than three members (count(current-group()) lt 3) by using a simplified version of our list of cars:

<?xml version="1.0"?>
<!-- carlist.xml -->
<cars>
  <make>Alfa Romeo</make>
  <make>Bentley</make>
  <make>Chevrolet</make>
  <make>Dodge</make>
  <make>Eagle</make>
  <make>Ford</make>
  <make>GMC</make>
  <make>Honda</make>
  <make>Isuzu</make>
  <model>Javelin</model>
  <model>K-Car</model>
  <make>Lincoln</make>
  <make>Mercedes</make>
  <make>Nash</make>
  <make>Opel</make>
  <make>Pontiac</make>
  <model>Quantum</model>
  <model>Rambler</model>
  <make>Studebaker</make>
</cars>

There are 19 cars in our list, so the final group should have only one item in it. Here’s the stylesheet:

<?xml version="1.0"?>
<!-- for-each-group_group-ending-with.xsl --> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" include-content-type="no"/> <xsl:template match="/"> <html> <head> <title>Car Makes and Models</title> </head> <body style="font-family: sans-serif;"> <h1>Car Makes and Models</h1> <p>Here are the car makes and models in our input document.</p> <table border="1" cellpadding="5"> <xsl:apply-templates ...

Get XSLT, 2nd 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.