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/*" ...
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.