Grouping

Hi,

how do i get this output??

thanks...
input:
 
<?xml version="1.0" encoding="UTF-8"?>
<content>
<CaseName>testing</CaseName>
<judgments>
<Judge>Andrew Ang J</Judge>
 
<para1>there is some text 1<para2>para 2 text 1</para2></para1>
<para1>there is some text 2</para1>
<para1>there is some text 3</para1>
<para1>there is some text 4</para1>
 
<heading1>first heading</heading1>
<para1>there is some text 5</para1>
 <Judge>Andrew Ang J</Judge>
<heading1>first heading</heading1>
<para1>there is some text 6</para1>
 
<heading2>second heading</heading2>
<para1>there is some text 7</para1>
<para1>there is some text 8</para1>
<para1>there is some text 9</para1>
 
<heading3>third heading</heading3>
<para1>there is some text 10<para2>para 2 text 2</para2></para1>
<heading3>third heading</heading3>
<para1>there is some text 10<para2>para 2 text 2</para2></para1>
 
</judgments>
</content>
 
 
output:
<content>
<CaseName>testing</CaseName>
<judgments>
<judgment>
<Judge>Andrew Ang J</Judge>
 
<para1>there is some text 1<para2>para 2 text 1</para2></para1>
<para1>there is some text 2</para1>
<para1>there is some text 3</para1>
<para1>there is some text 4</para1>
 <section>
<heading1>first heading</heading1>
<para1>there is some text 5</para1>
 <Judge>Andrew Ang J</Judge>
 </section>
 <section>
<heading1>first heading</heading1>
<para1>there is some text 6</para1>
 <section>
<heading2>second heading</heading2>
<para1>there is some text 7</para1>
<para1>there is some text 8</para1>
<para1>there is some text 9</para1>
  <section>
<heading3>third heading</heading3>
<para1>there is some text 10<para2>para 2 text 2</para2></para1>
 </section>
 <section>
<heading3>third heading</heading3>
<para1>there is some text 10<para2>para 2 text 2</para2></para1>
 </section>
 </section>
  </section>
</judgment>
</judgments>
</content>

Open in new window

KenTan85Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KenTan85Author Commented:
sry... this should be the output that i need:


output:
 
<content>
<CaseName>testing</CaseName>
<judgments>
<judgment>
<Judge>Andrew Ang J</Judge>
 
<para1>there is some text 1<para2>para 2 text 1</para2></para1>
<para1>there is some text 2</para1>
<para1>there is some text 3</para1>
<para1>there is some text 4</para1>
 <section>
<heading1>first heading</heading1>
<para1>there is some text 5</para1>
 </section>
</judgment>
<judgment>
 <Judge>Andrew Ang J 2</Judge>
 
 <section>
<heading1>first heading</heading1>
<para1>there is some text 6</para1>
 <section>
<heading2>second heading</heading2>
<para1>there is some text 7</para1>
<para1>there is some text 8</para1>
<para1>there is some text 9</para1>
  <section>
<heading3>third heading</heading3>
<para1>there is some text 10<para2>para 2 text 2</para2></para1>
 </section>
 <section>
<heading3>third heading</heading3>
<para1>there is some text 10<para2>para 2 text 2</para2></para1>
 </section>
 </section>
  </section>
</judgment>
</judgments>
</content>

Open in new window

Gertone (Geert Bormans)Information ArchitectCommented:
can you tell me what the trigger is for a new judgment?

Is it correct that
- the source has changed and the <judgment> has gone from the source, but you need it in the result
- the <judgment>s are created by encountering a <judge> element
KenTan85Author Commented:
yup.
new <judgment> are created whenever it encounter a new <judge> element
Gertone (Geert Bormans)Information ArchitectCommented:
Here we are,
there should be a simpler solution using apply-templates and mode, but use this for now
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
 
    <xsl:output indent="yes"></xsl:output>
    
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="judgments">
        <xsl:copy>
            <xsl:for-each-group select="*"  group-starting-with="Judge">
                <judgment>
                    <xsl:apply-templates select="self::Judge"/>
                    <xsl:for-each-group select="current-group() except self::Judge" group-starting-with="heading1">
                        <xsl:choose>
                            <xsl:when test="current-group()[self::heading1]">
                                <section>
                                    <xsl:apply-templates select="self::heading1"/>
                                    <xsl:for-each-group select="current-group() except self::heading1" group-starting-with="heading2">
                                        <xsl:choose>
                                            <xsl:when test="current-group()[self::heading2]">
                                                <section>
                                                    <xsl:apply-templates select="self::heading2"/>
                                                    <xsl:for-each-group select="current-group() except self::heading2" group-starting-with="heading3">
                                                        <xsl:choose>
                                                            <xsl:when test="current-group()[self::heading3]">
                                                                <section>
                                                                    <xsl:apply-templates select="self::heading3"/>
                                                                    <xsl:copy-of select="current-group() except self::heading3"/>
                                                                </section>
                                                            </xsl:when>
                                                            <xsl:otherwise>
                                                                <xsl:apply-templates select="current-group()"></xsl:apply-templates>
                                                            </xsl:otherwise>
                                                        </xsl:choose>
                                                    </xsl:for-each-group>
                                                </section>
                                            </xsl:when>
                                            <xsl:otherwise>
                                                <xsl:apply-templates select="current-group()"></xsl:apply-templates>
                                            </xsl:otherwise>
                                        </xsl:choose>
                                    </xsl:for-each-group>
                                </section>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:apply-templates select="current-group()"></xsl:apply-templates>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:for-each-group>
                </judgment>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>
 
</xsl:stylesheet>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.