Re: [xsl] grouping problem

Subject: Re: [xsl] grouping problem
From: M Balaji <msbalaji82@xxxxxxxxx>
Date: Wed, 22 Apr 2009 18:40:52 +0530
Ken, Thanks!!! It is working fine

On Wed, Apr 22, 2009 at 5:15 PM, G. Ken Holman
<gkholman@xxxxxxxxxxxxxxxxxxxx> wrote:
> At 2009-04-22 10:36 +0530, M Balaji wrote:
>>
>> I need to group the input.
>>
>> Input:
>> <root>
>>  <num>This chapter is..:</num>
>>  <test>A class is a module of Java code:</test>
>>  <section>or</section>
>>  <title>Java 5.0</title>
>>  <test>The Java programming language</test>
>>  <p>Write Once, Run Anywhere.</p>
>>  <num>Java j2ee</num>
>>  <p>Broad Responsibilities:...</p>
>>  <num>Job Description Perform Oracle...</num>
>> </root>
>>
>> Expected output:
>>
>> <msgs>
>>  <msg>
>>     <num>This chapter is..:</num>
>>     <import>
>>        <test>A class is a module of Java code:</test>
>>        <title>Java 5.0</title>
>>        <section>or</section>
>
> Are the above two lines supposed to be inverted?  I'm assuming not.
>
>>        <test>The Java programming language</test>
>>     </import>
>>     <p>Write Once, Run Anywhere.</p>
>>  </msg>
>>  <msg>
>>     <num>Java j2ee</num>
>>     <p>Broad Responsibilities:...</p>
>>  </msg>
>>  <msg>
>>     <num>Job Description Perform Oracle...</num>
>>  </msg>
>> </msgs>
>>
>> I tried with the below stylesheet.
>>
>> <xsl:template match="root">
>> <msgs>
>> <xsl:for-each-group select="*"
>> group-starting-with="*[starts-with(name(), 'num')]">
>> <msg>
>> <xsl:apply-templates select="." mode="msg"/>
>> <xsl:for-each-group select="current-group() except ."
>> group-ending-with="test[last()]">
>> <xsl:apply-templates select="." mode="msg"/>
>> <import>
>> <xsl:apply-templates select="current-group() except ." mode="msg"/>
>> </import>
>> </xsl:for-each-group>
>> </msg>
>> </xsl:for-each-group>
>> </msgs>
>> </xsl:template>
>>
>> Output received:-
>
> The following is not the output I get with your stylesheet.
>
>> <msgs>
>>   <msg>
>>      <num>This chapter is..:</num>
>>      <import>
>>         <test>A class is a module of Java code:</test>
>>         <section>or</section>
>>         <title>Java 5.0</title>
>>         <test>The Java programming language</test>
>>      </import>
>>      <import><!--Import tag need to remove -->
>>         <p>Write Once, Run Anywhere.</p>
>>      </import>
>>   </msg>
>>   <msg>
>>      <num>Java j2ee</num>
>>      <import> <!-- import tag need to remove -->
>>         <p>Broad Responsibilities:...</p>
>>      </import>
>>   </msg>
>>   <msg>
>>      <num>Job Description Perform Oracle...</num>
>>   </msg>
>> </msgs>
>>
>> Where I need to modify the stylesheet to get the expected output?
>
> The first step is to get the intermediate results above, which is done with
> the following changes to your stylesheet:
>
> <?xml version="1.0" encoding="US-ASCII"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>                version="2.0">
>
> <xsl:output indent="yes"/>
>
> <xsl:template match="root">
> <msgs>
> <xsl:for-each-group select="*"
> group-starting-with="*[starts-with(name(), 'num')]">
> <msg>
> <xsl:apply-templates select="." mode="msg"/>
> <xsl:for-each-group select="current-group() except ."
> group-ending-with="test[last()]">
> <import>
> <xsl:apply-templates select="current-group()" mode="msg"/>
> </import>
> </xsl:for-each-group>
> </msg>
> </xsl:for-each-group>
> </msgs>
> </xsl:template>
>
> <xsl:template match="@*|node()" mode="msg">
>  <xsl:copy>
>    <xsl:apply-templates select="@*|node()"/>
>  </xsl:copy>
> </xsl:template>
>
> </xsl:stylesheet>
>
> ... and then it can be seen how to fix the problem.  To fix the problem you
> need only wrap an <import> group when there is a <test> element inside,
> otherwise put out the group without the wrapper.
>
> I hope the example below helps.
>
> . . . . . . . . . . . Ken
>
>
> T:\ftemp>type balaji.xml
> <root>
>  <num>This chapter is..:</num>
>  <test>A class is a module of Java code:</test>
>  <section>or</section>
>  <title>Java 5.0</title>
>  <test>The Java programming language</test>
>  <p>Write Once, Run Anywhere.</p>
>  <num>Java j2ee</num>
>  <p>Broad Responsibilities:...</p>
>  <num>Job Description Perform Oracle...</num>
> </root>
>
> T:\ftemp>call xslt2 balaji.xml balaji.xsl
> <?xml version="1.0" encoding="UTF-8"?>
> <msgs>
>   <msg>
>      <num>This chapter is..:</num>
>      <import>
>         <test>A class is a module of Java code:</test>
>         <section>or</section>
>         <title>Java 5.0</title>
>         <test>The Java programming language</test>
>      </import>
>      <p>Write Once, Run Anywhere.</p>
>   </msg>
>   <msg>
>      <num>Java j2ee</num>
>      <p>Broad Responsibilities:...</p>
>   </msg>
>   <msg>
>      <num>Job Description Perform Oracle...</num>
>   </msg>
> </msgs>
> T:\ftemp>type balaji.xsl
> <?xml version="1.0" encoding="US-ASCII"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>                version="2.0">
>
> <xsl:output indent="yes"/>
>
> <xsl:template match="root">
>  <msgs>
>    <xsl:for-each-group select="*"
>                        group-starting-with="*[starts-with(name(), 'num')]">
>      <msg>
>        <xsl:apply-templates select="." mode="msg"/>
>        <xsl:for-each-group select="current-group() except ."
>                            group-ending-with="test[last()]">
>          <xsl:choose>
>            <xsl:when test="current-group()/self::test">
>              <import>
>                <xsl:apply-templates select="current-group()"
>                                     mode="msg"/>
>              </import>
>            </xsl:when>
>            <xsl:otherwise>
>              <xsl:apply-templates select="current-group()"
>                                   mode="msg"/>
>            </xsl:otherwise>
>          </xsl:choose>
>        </xsl:for-each-group>
>      </msg>
>    </xsl:for-each-group>
>  </msgs>
> </xsl:template>
>
> <xsl:template match="@*|node()" mode="msg">
>  <xsl:copy>
>    <xsl:apply-templates select="@*|node()"/>
>  </xsl:copy>
> </xsl:template>
>
> </xsl:stylesheet>
> T:\ftemp>rem Done!
>
>
>
> --
> XSLT/XSL-FO/XQuery hands-on training - Los Angeles, USA 2009-06-08
> Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
> Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
> Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
> G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
> Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
> Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
> Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread