以下のような入力HTMLを、一連の兄弟要素をグルーピングする目的でXSLT1.0で処理したいと思っています。
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<div>
<p>A</p>
<div class="case">0</div>
<div class="case">1</div>
<div class="case">2</div>
<p>B</p>
<p>C</p>
<div class="case">3</div>
<div class="case">4</div>
<p>D</p>
<p>E</p>
<div class="case">5</div>
<div class="case">6</div>
</div>
</body>
</html>
期待する出力は以下のようなものです。
<?xml version="1.0"?>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<div>
<p>A</p>
<box>
<p>0</p>
<p>1</p>
<p>2</p>
</box>
<p>B</p>
<p>C</p>
<box>
<p>3</p>
<p>4</p>
</box>
<p>D</p>
<p>E</p>
<box>
<p>5</p>
<p>6</p>
</box>
</div>
</body>
</html>
以下のXSLを書いてみましたが、一番下の<xsl:template match="div[contains(@class, 'case')]" />
が効いてしまい、boxの中身が空になってしまいます。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="div[contains(@class, 'case')][not(preceding-sibling::*[1][contains(@class, 'case')])]">
<box>
<xsl:apply-templates select="." mode="inbox" />
<xsl:apply-templates select="following-sibling::div[contains(@class, 'case')][preceding-sibling::*[not(self::div[contains(@class, 'case')])][1]/following-sibling::div[contains(@class, 'case')][1] = current()]" mode="inbox" />
</box>
</xsl:template>
<xsl:template match="div[contains(@class, 'case')]" mode="inbox">
<p><xsl:apply-templates select="node()" /></p>
</xsl:template>
<xsl:template match="div[contains(@class, 'case')]" />
</xsl:stylesheet>
よろしければ、アドバイスお願いします。