O'Reilly logo

XSLT, 2nd Edition by Doug Tidwell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Another Type of Grouping: group-adjacent

With the group-adjacent approach, we’ll create a group based on some number of elements that are together in the source document. Our example input document is an HTML document that features groups of paragraphs together:

<?xml version="1.0"?>
<!-- group-adjacent_input.html -->
<html>
  <body>
    <h2>Steps for grouping in the Muench method</h2>
    <p>Define a <code>key</code> for the property we want 
    to use for grouping.</p>
    <p>Select all of the nodes ...</p>
    <p>For each unique grouping value, ...</p>
    <h2>Steps for grouping in XSLT 2.0</h2>
    <p>Define an XPath expression ...</p>
    <p>Select all of the nodes we want to group ...</p>
    <p>Instead of dealing with each ...</p>
  </body>
</html>

This is text from earlier in this chapter, displayed here as HTML. (This book is written entirely in DocBook, an XML vocabulary with a very well-defined structure.) What we want to do is convert any sequence of paragraphs into an unordered list (<ul>), with each paragraph converted into a list item (<li>). We’ll use group-adjacent to do that. Our stylesheet looks like this:

<?xml version="1.0"?>
<!-- for-each-group_group-adjacent.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>Grouping with group-adjacent</title> </head> <body style="font-family: sans-serif;"> <h1>Grouping with group-adjacent</h1> <xsl:for-each-group select="html/body/*" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required