I'm working with a XML file that must be TEI-compilant. The problem is about the pb (page break) milestone. It's not a new problem, but the existing solutions are so complicated and heavy that I was wondering if there was a more simple way in my case.
Let's have this XML file part :
<body>
<pb n="2"/><p>Some random text is put here</p><p>Another
paragraph starts here, and in the
middle of it<pb n="3"/> a page break occurred.</p><pb n="4"/
<p>the next paragraph begins
on a new page</p>
<p>But in the next paragraph, after<pb n="5"/>another page break, something else
happend : a note<note
type=glossary">the note content</note> and so everything
failing <pb n="6"/> because of this note.
I would like to transform the XML into this HTML :
<table>
<tr><td><p>Some random text is put here</p><p>Another paragraph starts here, and in the
middle of it</p></td><td>2</td></tr>
<tr><td><p>a page break occurred.</p></td><td>3</td></tr>
<tr><td><p>the next par graph begins on a new page</p></td><td>4</td></tr>
<tr><td><p>But in the next paragraph, after</td><td>5</td></tr>
<tr><td><p>another page break, something else happend : a note
<note type=glossary">the note content</note> and so everything's failing</td><td>6</td></tr>
<tr><td>because of this note.</td></tr>
It seems to me that it should be possible to achieve quite simply with a for-each-group. So, basically, I was trying something like that :
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:template match="/body">
<table>
<xsl:for-each-group select="descendant::*" group-starting-with="pb">
<tr><td><xsl:value-of select="current-group()"/>
</td><td><xsl:value-of select="current-group()[1]/@n"/>
</td></tr>
</xsl:for-each-group>
</table>
</xsl:template>
</xsl:stylesheet>
Obviously, it doesn't work… the result is :
<table><tr><td> Some random text is put here Another paragraph starts here, and in the
middle of it a page break occurred.</td><td>2</td></tr>
<tr><td/><td>3</td></tr><tr><td>
the next paragraph begins on
a new page</td><td>4</td></tr></table>
Am I going in a totally wrong direction ?
Many thanks for your help !
Christophe
I think you are not in wrong direction with for-each-group. Sometimes it is useful (for me) to make some "preprocessing" of input document and save it into variable for further transformation.
I have this xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="fn xs fo">
<xsl:output method="html" />
<!-- Make some "preprocess" - just to splip everything containing pb -->
<xsl:variable name="preprocess">
<!-- Only root element shouldn't be splitted -->
<xsl:element name="{/node()[1]/name()}">
<xsl:apply-templates select="/node()[1]/node() | @*" mode="preprocess"/>
</xsl:element>
</xsl:variable>
<xsl:template match="node() | @*" mode="preprocess">
<xsl:copy>
<xsl:apply-templates select="node() | @*" mode="preprocess"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[pb]" mode="preprocess">
<!-- I don't know if pb could be in another element than p - so do it more generic -->
<xsl:variable name="nodeName" select="name()" />
<xsl:element name="{$nodeName}">
<!-- Have to account there could be more pb elements - working with 1st of them -->
<xsl:apply-templates select="pb[1]/preceding-sibling::node()" mode="preprocess" />
</xsl:element>
<xsl:copy-of select="pb[1]" />
<!-- I have to continue with the rest of element - I store it into another variable
an encapsulate it with the element of the same name. Then it is processing
in standard way. -->
<xsl:variable name="restOfElement">
<xsl:element name="{$nodeName}">
<xsl:copy-of select="pb[1]/following-sibling::node()" />
</xsl:element>
</xsl:variable>
<xsl:apply-templates select="$restOfElement" mode="preprocess" />
</xsl:template>
<!-- Apply for-each-group on preprocessed value -->
<xsl:template match="/">
<html>
<head>
<title></title>
</head>
<body>
<table>
<xsl:for-each-group select="$preprocess/body/descendant::*" group-starting-with="pb">
<tr>
<td>
<xsl:copy-of select="current-group()[position() > 1]" />
</td>
<td><xsl:value-of select="current-grouping-key()/@n"/></td>
</tr>
</xsl:for-each-group>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
In first step I split all elements containing <pb>
. The variable then looks like following
<body>
<pb n="2"/>
<p>Some random text is put here</p>
<p>Another
paragraph starts here, and in the
middle of it</p>
<pb n="3"/>
<p> a page break occurred.</p>
<pb n="4"/>
<p>the next paragraph begins
on a new page</p>
<p>But in the next paragraph, after</p>
<pb n="5"/>
<p>another page break, something else
happend : a note<note type="glossary">the note content</note> and so everything
failing </p>
<pb n="6"/>
<p> because of this note</p>
</body>
On this I apply your for-each-group statement. It produced following output
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<table>
<tr>
<td>
<p>Some random text is put here</p>
<p>Another
paragraph starts here, and in the
middle of it</p>
</td>
<td>2</td>
</tr>
<tr>
<td>
<p> a page break occurred.</p>
</td>
<td>3</td>
</tr>
<tr>
<td>
<p>the next paragraph begins
on a new page</p>
<p>But in the next paragraph, after</p>
</td>
<td>4</td>
</tr>
<tr>
<td>
<p>another page break, something else
happend : a note
<note type="glossary">the note content</note> and so everything
failing </p>
<note type="glossary">the note content</note>
</td>
<td>5</td>
</tr>
<tr>
<td>
<p> because of this note</p>
</td>
<td>6</td>
</tr>
</table>
</body>
</html>