[Home] [By Thread] [By Date] [Recent Entries]
Hi Byomkesh, From what I could gather from your source XML and the desired output, I think the following stylesheet should work: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="xml" indent="yes" /> <xsl:template match="/html">
<document>
<xsl:apply-templates select="body" />
</document>
</xsl:template><xsl:template match="body">
<xsl:for-each-group select="div/*" group-starting-with="h1">
<section1>
<title><xsl:value-of select="current-group()[1]" /></title>
<xsl:for-each-group select="current-group()[position() >
1]" group-ending-with="h2">
<xsl:if test="position() = 1">
<xsl:for-each select="current-group()[position() <
last()]">
<para><xsl:value-of select="." /></para>
</xsl:for-each>
<section2>
<title><xsl:value-of select="current-group()[last()]" /></title>
<xsl:for-each-group
select="current-group()[last()]/following-sibling::* |
current-group()[last()]/../following-sibling::*/*"
group-ending-with="h3">
<xsl:if test="position() = 1">
<xsl:for-each select="current-group()[position() < last()]">
<para><xsl:value-of select="." /></para>
</xsl:for-each>
<section3>
<title><xsl:value-of
select="current-group()[last()]" /></title>
<xsl:for-each-group
select="current-group()[last()]/following-sibling::* |
current-group()[last()]/../following-sibling::*/*"
group-ending-with="h1 | p[not(following-sibling::*[1]) and
not(../following-sibling::div)]">
<xsl:if test="position() = 1">
<xsl:for-each
select="current-group()[position() < last()]">
<para><xsl:value-of select="." /></para>
</xsl:for-each>
<xsl:if test="current-group()[last()]/self::p">
<para><xsl:value-of
select="current-group()[last()]" /></para>
</xsl:if>
</xsl:if>
</xsl:for-each-group>
</section3>
</xsl:if>
</xsl:for-each-group>
</section2>
</xsl:if>
</xsl:for-each-group>
</section1>
</xsl:for-each-group>
</xsl:template></xsl:stylesheet> With this source XML: <html> <body> <div> <h1>Heading 1.</h1> <p>Some text here..</p> <p>Sahoo</p> </div> <div> <h2>Heading 2...</h2> <p>text here....</p> </div> <div> <h3>Heading 3.....</h3> <p>Some text here......</p> </div> <div> <h1>Again Heading 1.</h1> <p>Some text here..</p> <p>Introduction to Wi-Fi</p> </div> <div> <h2>Again Heading 2...</h2> <p>text here....</p> <p>History and Basics</p> </div> <div> <h3>Again Heading 3.....</h3> <p>Some text here......</p> </div> </body> </html> The following desired output is produced: <?xml version="1.0" encoding="UTF-8"?>
<document>
<section1>
<title>Heading 1.</title>
<para>Some text here..</para>
<para>Sahoo</para>
<section2>
<title>Heading 2...</title>
<para>text here....</para>
<section3>
<title>Heading 3.....</title>
<para>Some text here......</para>
</section3>
</section2>
</section1>
<section1>
<title>Again Heading 1.</title>
<para>Some text here..</para>
<para>Introduction to Wi-Fi</para>
<section2>
<title>Again Heading 2...</title>
<para>text here....</para>
<para>History and Basics</para>
<section3>
<title>Again Heading 3.....</title>
<para>Some text here......</para>
</section3>
</section2>
</section1>
</document>Regards, Mukul On 6/29/06, Byomkesh <bkesh@xxxxxxxxxxxxxxx> wrote: Dear Mukul,
|

Cart



