I have the following XSLT 2.0 code to split an XHTML file into chapters:
<xsl:for-each-group
select=".//html:*[local-name() eq $chapter-tag][1]/(.|following-sibling::*)"
group-starting-with="html:*[local-name() eq $chapter-tag]">
...
</xsl:for-each-group>
(here $chapter-tag
is either h1
or h2
).
But this code does not work for the following XHTML fragment:
<div class="header">
<h1>Header</h1>
</div>
<p>...</p>
...
Please help to do the right thing when the header is "buried" inside other tags.
Complete example:
<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
</head>
<body>
<div class="container">
<div class="header">
<h1>First chapter</h1>
</div>
<p>First chapter text.</p>
<p>Blah, blah, blah...</p>
<div class="header">
<h1>Second chapter</h1>
</div>
<p>Second chapter text.</p>
<p>Blah, blah, blah...</p>
</div>
</body>
</html>
This should create the following element groups ("chapters"):
<div class="header">
<h1>First chapter</h1>
</div>
<p>First chapter text.</p>
<p>Blah, blah, blah...</p>
and
<div class="header">
<h1>Second chapter</h1>
</div>
<p>Second chapter text.</p>
<p>Blah, blah, blah...</p>