I have the following XML from which I am trying to generate HTML files.
<?xml version="1.0" encoding="UTF-8"?>
<text>
<body>
<milestone unit="fragment"/>
<div>
<p>First line text in FIRST fragment</p>
<p>Second line <seg>text in FIRST</seg> fragment</p>
<p>Third line text in FIRST fragment</p>
<milestone unit="fragment"/>
<p>First line text in SECOND fragment</p>
<p>Second line <seg>text in SECOND </seg> fragment</p>
<p>Third line text in SECOND fragment</p>
<milestone unit="fragment"/>
<p>First line text in THIRD fragment</p>
<p>Second line <seg>text in THIRD </seg> fragment</p>
<p>Third line text in THIRD fragment</p>
</div>
</body>
</text>
Expected HTML Output:
HTML 1:
<p>First line text in FIRST fragment</p>
<p>Second line <span>text in FIRST</span>fragment</p>
<p>Third line text in FIRST fragment</p>
HTML 2:
<p>First line text in SECOND fragment</p>
<p>Second line <span>text in SECOND </span> fragment</p>
<p>Third line text in SECOND fragment</p>
HTML 3:
<p>First line text in THIRD fragment</p>
<p>Second line <span>text in THIRD </span> fragment</p>
<p>Third line text in THIRD fragment</p>
This is the XSLT where I am using for-each-group to group the nodes between “milestone” tags, but the grouping does not occur as expected.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:output method="html" name="html" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" include-content-type="no"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="body">
<xsl:for-each-group select="descendant::*" group-starting-with="milestone[@unit='fragment']">
<xsl:variable name="currFragNumber">
<xsl:number count="milestone[@unit='fragment']" level="any" from="text"/>
</xsl:variable>
<xsl:result-document href="{$currFragNumber}.html" format="html">
<xsl:apply-templates select="current-group()" />
</xsl:result-document>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="p">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template match="seg">
<span>
<xsl:apply-templates/>
</span>
</xsl:template>
</xsl:stylesheet>
Below is the output after of the transform.
HTML 1:
<p>First line text in FIRST fragment</p>
<p>Second line <span>text in FIRST</span>fragment</p>
<p>Third line text in FIRST fragment</p>
<p>First line text in SECOND fragment</p>
<p>Second line <span>text in SECOND</span>fragment</p>
<p>Third line text in SECOND fragment</p>
<p>First line text in THIRD fragment</p>
<p>Second line <span>text in THIRD</span>fragment</p>
<p>Third line text in THIRD fragment</p>
<p>First line text in FIRST fragment</p>
<p>Second line <span>text in FIRST</span>fragment</p>
<span>text in FIRST</span>
<p>Third line text in FIRST fragment</p>
HTML 2:
<p>First line text in SECOND fragment</p>
<p>Second line <span>text in SECOND </span> fragment</p>
<span>text in SECOND </span>
<p>Third line text in SECOND fragment</p>
HTML 3:
<p>First line text in THIRD fragment</p>
<p>Second line <span>text in THIRD </span> fragment</p>
<span>text in THIRD </span>
<p>Third line text in THIRD fragment</p>
I am trying to understand why the first current-group()(i.e. first “milestone”) is matching the whole document rather than just the nodes between the “milestone” tags.
The problem is that you are also selecting the
div, as I’ve suggested in comments.Use:
It will correctly serialize three result documents. Do note that you could just forget the first
milestonebecause according to http://www.w3.org/TR/xslt20/#xsl-for-each-group*: emphasis is mine.