Hello,
I have problem with converting a part of an XML file into more
hierarchically structured XML.
My guess is that multilevel grouping with each-for-group is the method I
should use, but I tried several methods - group-by, group-starting-with,
and group-adjacent and it worked partially. The first "Chapter" level
works as it should, but can't process the lower levels. I want to
replicate the sequence in the list, but I want the output to be properly
nested.
The other problem is that for some files the string will start with
letter, sometime with a number, e.g. Part 1; A/1; 1/7). I tried to use
regular expressions by they did not work.
Thanks in advance,
Standa
my input xml file looks like this:
<book>
<title>SomeTitle</title>
<description>SomeDescription</description>
<b_part><level_name>Chapter I: Title of Chapter I</level_name></b_part>
<b_part><level_name>Section 1: Title of section 1</level_name></b_part>
<b_part><level_name>Part 1: Title of Part 1</level_name></b_part>
<b_part><level_name>Part 2: Title of Part 2</level_name></b_part>
<b_part><level_name>Part n: Title of Part n </level_name></b_part>
<b_part><level_name>Section 2: Title of Part 2</level_name></b_part>
<b_part><level_name>Part 1: Title of Part 1</level_name></b_part>
<b_part><level_name>Part n: Title of Part n</level_name></b_part>
<b_part><level_name>Chapter II: Title of Chapter II</level_name></b_part>
<b_part><level_name>A) Title of Section A</level_name></b_part>
<b_part><level_name>A/1 Title of Part 1</level_name></b_part>
<b_part><level_name>A/n Title of Part n</level_name></b_part>
<b_part><level_name>B) Title of Section A</level_name></b_part>
<b_part><level_name>B/1 Title of Part 1</level_name></b_part>
<b_part><level_name>B/n Title of Part n</level_name></b_part>
</book>
my stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
<xsl:output method="xml" encoding="UTF-8" name="xml" indent="yes"/>
<xsl:template match="/">
<book>
<xsl:apply-templates select="book"/>
</book>
</xsl:template>
<xsl:template match="book">
<xsl:for-each select="./title">
<title>
<xsl:value-of select="."/>
</title>
</xsl:for-each>
<xsl:if test="b_part">
<xsl:call-template name="toc_xml"/>
</xsl:if>
</xsl:template>
<xsl:template name="toc_xml">
<toc>
<p>Table of contents</p>
<xsl:for-each-group select="b_part"
group-by="./level_name[starts-with(.,'Chapter')]">
<xsl:call-template name="level-01"/>
</xsl:for-each-group>
</toc>
</xsl:template>
<xsl:template name="level-01">
<chapter>
<title>
<xsl:value-of select="./level_name"/>
</title>
<xsl:for-each-group select="current-group() except ."
group-starting-with="b_part/level_name[starts-with(.,'Section')]">
<xsl:call-template name="level-02"/>
</xsl:for-each-group>
</chapter>
</xsl:template>
<xsl:template name="level-02">
<section>
<title>
<xsl:value-of select="./level_name"/>
</title>
<xsl:for-each-group select="current-group() except ."
group-starting-with="b_part/level_name[starts-with(.,'[A-Z]/[0-9]')]">
<xsl:call-template name="level-03"/>
</xsl:for-each-group>
</section>
</xsl:template>
<xsl:template name="level-03">
<part>
<title>
<xsl:value-of select="./level_name"/>
</title>
</part>
</xsl:template>
</xsl:stylesheet>
desired output:
<book>
<title>Some title</title>
<toc> <p>paragraph</p>
<chapter><title>Chapter I: Title of Chapter I</title>
<section><title>Section 1: Title of Section 1</title>
<part><title>Part 1: Title of Part 1</title></part>
<part><title>Part 2: Title of Part 2</title></part>
<part><title>Part n: Title of Part n</title></part>
</section>
<section><title>Section 2: Title of Section 2</title></section>
<part><title>Part 1: Title of Part 1</title></part>
<part><title>Part n: Title of Part n</title></part>
</chapter>
<chapter><title>Chapter II: Title of Chapter II</title>
<section><title>A) Title of Section A</title>
<part><title>A/1 Title of Part 1</title></part>
<part><title>A/n Title of Part n </title></part>
</section>
<section><title>B) Title of Section B</title>
<part><title>B/1 Title of Part 1</title></part>
<part><title>B/n Title of Part n </title></part>
</section>
</chapter>
</toc>>
</book>
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--