I have some xml documents (similar to docbook) that have to be transformed to xsl-fo. Some of the documents contains poems, and the lines of the poems are written in separate p
This stylesheet:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="div[@class='poem']">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:for-each-group select="*" group-ending-with="br|h4">
<div class="strophe">
<xsl:copy-of select="current-group()/self::p[not(@class)]"/>
</div>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
With this input:
<div class="poem">
<h4>Headline</h4>
<p>1st line of 1st verse</p>
<p>2nd line of 1st verse</p>
<br/>
<p>1st line of 2nd verse</p>
<p>2nd line of 2nd verse</p>
<page n="100"/>
<p>3rd line of 2nd verse</p>
</div>
Output:
<div class="poem">
<div class="strophe">
<p>1st line of 1st verse</p>
<p>2nd line of 1st verse</p>
</div>
<div class="strophe">
<p>1st line of 2nd verse</p>
<p>2nd line of 2nd verse</p>
<p>3rd line of 2nd verse</p>
</div>
</div>
With this input:
<div class="poem">
<h4>Headline</h4>
<p class="center">1</p>
<p>1st line of 1st verse</p>
<p>2nd line of 1st verse</p>
<br/>
<p class="center">2</p>
<p>1st line of 2nd verse</p>
<p>2nd line of 2nd verse</p>
<page n="100"/>
<p>3rd line of 2nd verse</p>
</div>
Output:
<div class="poem">
<div class="strophe">
<p>1st line of 1st verse</p>
<p>2nd line of 1st verse</p>
</div>
<div class="strophe">
<p>1st line of 2nd verse</p>
<p>2nd line of 2nd verse</p>
<p>3rd line of 2nd verse</p>
</div>
</div>
So, this stylesheet:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="div[@class='poems']">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:for-each-group select="*[preceding-sibling::h4]"
group-starting-with="h4">
<div class="poem">
<xsl:for-each-group select="current-group()"
group-ending-with="br">
<div class="strophe">
<xsl:copy-of select="current-group()
/self::p[not(@class)]"/>
</div>
</xsl:for-each-group>
</div>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
With this input:
<div class="poems">
<h3>Poems</h3>
<h4>Headline</h4>
<p>1st line of 1st verse</p>
<p>2nd line of 1st verse</p>
<br/>
<p>1st line of 2nd verse</p>
<p>2nd line of 2nd verse</p>
<page n="100"/>
<p>3rd line of 2nd verse</p>
<h4>Headline</h4>
<p class="center">1</p>
<p>1st line of 1st verse</p>
<p>2nd line of 1st verse</p>
<br/>
<p class="center">2</p>
<p>1st line of 2nd verse</p>
<p>2nd line of 2nd verse</p>
<page n="100"/>
<p>3rd line of 2nd verse</p>
</div>
Output:
<div class="poems">
<div class="poem">
<div class="strophe">
<p>1st line of 1st verse</p>
<p>2nd line of 1st verse</p>
</div>
<div class="strophe">
<p>1st line of 2nd verse</p>
<p>2nd line of 2nd verse</p>
<p>3rd line of 2nd verse</p>
</div>
</div>
<div class="poem">
<div class="strophe">
<p>1st line of 1st verse</p>
<p>2nd line of 1st verse</p>
</div>
<div class="strophe">
<p>1st line of 2nd verse</p>
<p>2nd line of 2nd verse</p>
<p>3rd line of 2nd verse</p>
</div>
</div>
</div>