[xsl] Re: Grouping Problem in Hedear Tag

Subject: [xsl] Re: Grouping Problem in Hedear Tag
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Fri, 30 Jun 2006 23:43:25 +0530
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() &gt;
1]" group-ending-with="h2">
         <xsl:if test="position() = 1">
           <xsl:for-each select="current-group()[position() &lt;
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() &lt; 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() &lt; 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,

I am so confused in Grouping style. I am new about apply Group. Some text
(next <h1> text) is missing in my output files. So Please Check my files....
My following Xml file is

XML File
------------

<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>
<!-- If i am creat another <h2> text here, its not converting properly. -->

</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>

and My XSL file
--------------------

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0">

<xsl:output method="xml" indent="yes" />

<xsl:template match="body">
  <document>
      <xsl:for-each-group select="*" group-starting-with="h1">
         <section1>
   <title><xsl:value-of select="current-group()[1]/h1" /></title>
 <xsl:choose>
  <xsl:when test="p">
  <xsl:apply-templates select="p"/>
  </xsl:when>

 </xsl:choose>
   <xsl:for-each-group select="current-group()[position() &gt; 1]"
group-starting-with="h2">
                 <section2>
   <title><xsl:value-of select="current-group()[1]/h2"/></title>
 <xsl:choose>
  <xsl:when test="p">
  <xsl:apply-templates select="p"/>
  </xsl:when>

 </xsl:choose>
   <xsl:for-each-group select="current-group()[position() &gt; 1]"
group-starting-with="h3">
         <section3>
         <title><xsl:value-of select="current-group()[1]/h3" /></title>
 <xsl:choose>
  <xsl:when test="p">
  <xsl:apply-templates select="p"/>
  </xsl:when>

</xsl:choose>

         </section3>
                     </xsl:for-each-group>
 </section2>
             </xsl:for-each-group>
         </section1>
      </xsl:for-each-group>
  </document>
</xsl:template>

<xsl:template match="p">
<para>
<xsl:apply-templates/>
</para>
</xsl:template>

</xsl:stylesheet>

Now my out put ---------

<?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>
<!-- Missing in Next <h1> text.. -->

</document>

Please suggest me, what is wrong.

Thanks and Regards

Byomkesh

Current Thread