Hi, I am trying to solve a problem using XSLT 2. Here is my input:
<?xml version="1.0" encoding="UTF-8"?>
<book>
<chapter>
<title>Chapter 1</title>
<section>
<title>Section 1.1</title>
</section>
<section>
<title>Section 1.2</title>
</section>
<section>
<title>Section 1.3</title>
<section>
<title>Section 1.3.1</title>
</section>
<section>
<title>Section 1.3.2</title>
</section>
<section>
<title>Section 1.3.3</title>
</section>
<section>
<title>Section 1.3.4</title>
<intro/>
<section>
<title>Section 1.3.4.1</title>
</section>
<section>
<title>Section 1.3.4.2</title>
</section>
<section>
<title>Section 1.3.4.3</title>
</section>
</section>
</section>
</chapter>
<chapter>
<title>Chapter 2</title>
<section>
<title>Section 2.1</title>
<section>
<title>Section 2.1.1</title>
</section>
<section>
<title>Section 2.1.2</title>
<intro/>
<section>
<title>Section 2.1.2.1</title>
</section>
<section>
<title>Section 2.1.2.2</title>
</section>
<section>
<title>Section 2.1.2.3</title>
</section>
</section>
</section>
<section>
<title>Section 2.2</title>
</section>
</chapter>
</book>
I want to split this content up into separate files (eventually HTML). Here
is the desired output:
<?xml version="1.0" encoding="UTF-8"?>
<book>
<file>
<title>Chapter 1</title>
<section>
<title>Section 1.1</title>
</section>
</file>
<file>
<title>Section 1.2</title>
</file>
<file>
<title>Section 1.3</title>
<section>
<title>Section 1.3.1</title>
</section>
</file>
<file>
<title>Section 1.3.2</title>
</file>
<file>
<title>Section 1.3.3</title>
</file>
<file>
<!-- Doesn't have to be with the following child section because it
has <intro/> content. -->
<title>Section 1.3.4</title>
<intro/>
</file>
<file>
<title>Section 1.3.4.1</title>
</file>
<file>
<title>Section 1.3.4.2</title>
</file>
<file>
<title>Section 1.3.4.3</title>
</file>
<file>
<!-- All have to stay together because there is no content between
the <title/> and child <section/> -->
<title>Chapter 2</title>
<section>
<title>Section 2.1</title>
<section>
<title>Section 2.1.1</title>
</section>
</section>
</file>
<file>
<title>Section 2.1.2</title>
<intro/>
</file>
<file>
<title>Section 2.1.2.1</title>
</file>
<file>
<title>Section 2.1.2.2</title>
</file>
<file>
<title>Section 2.1.2.3</title>
</file>
<file>
<title>Section 2.2</title>
</file>
</book>
The rules are as follows: A <section> can be in its own file if
1) There are no child <section> elements (like Section 1.2).
2) There is element content after the <title> and before the first child
<section> element; for example an <intro> element (like Section 1.3.4).
Otherwise, the <section> has to contain any first child descendants (like
Chapter 2 through Section 2.1.1).
This looks to me like a grouping problem where I can use
<xsl:for-each-group>. I have this as a shell but I want to make sure I am on
the right track. Thank you very much.
<?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:template match="/">
<xsl:for-each-group group-starting-with="//chapter|//section"
select=".">
<file>
<xsl:copy-of select="current-group()"/>
</file>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
Rick
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--