• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 162
  • Last Modified:

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

0
KenTan85
Asked:
KenTan85
  • 2
  • 2
1 Solution
 
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

0
 
Geert BormansCommented:
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
0
 
KenTan85Author Commented:
yup.
new <judgment> are created whenever it encounter a new <judge> element
0
 
Geert BormansCommented:
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

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now