I'm trying to break up a large XML book into multiple files (by chapter). This sandbox XML:
<?xml version="1.0" encoding="UTF-8"?>
<book>
<chapter id='1'>Chapter 1</chapter>
<p>Highly random content.</p>
<div>foo</div>
<blockquote>Bar</blockquote>
<chapter id='2'>Chapter 2</chapter>
<div>
<p>p in <i>italicized div</i>!</p>
</div>
<p>Content.</p>
<chapter id='3'>Chapter 3</chapter>
<blockquote>foo</blockquote>
<p>foo</p>
<chapter id='4'>Chapter 4</chapter>
<p>foo</p>
<p>end of book</p>
</book>
Transforms as desired by this XSLT:
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="book">
<xsl:for-each-group select="*" group-starting-with="chapter">
<xsl:result-document href="{concat('/home/paul/t',@id,'.xhtml')}">
<xsl:for-each select="current-group()[self::*]">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:result-document>
</xsl:for-each-group>
</xsl:template>
Problem. When I apply the XSLT to a more complex production document, it doesn't work. I'd put the source doc here..but it's too large, and the client may not like that anyway.
I'm puzzled as to why it doesn't work. Instead of making 1 file per chapter like the sandbox test, it makes one large file. Suspect the problem may be related to the @id in the filename. On a production document, the @id comes up null. The result file name comes out as t.xhtml with the entire book in it, when I really wanted multiple files of t --id of the chapter-- .xhtml.
Any hints where to look? Is there a way I could make the code above more resilent to varying document complexity (I think that is my problem...the move from the simple concept to production has introduced something I'm not catching, maybe some tighter code would help).
chapterelements you are trying to match with the patterngroup-starting-with="chapter"are not in some namespace? If the result is a single file then it looks as if the grouping creates a single group and that would happen if the pattern does not match at all. - Martin Honnen<chapter>. This happens if either the document does not contain any chapter OR if there are other tags under<book>but before the first<chapter>. These will be selected as one group without@ID. At least that's what Saxon does for your input file with extra preceeding tags inserted. I second @Martin: we need some (at least shortened) realistic input. - Marcus Rickert