Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to group the data based on element and populate it.

below mentioned is input xml and if you see there are 4 rows for P and then Head element starts that's where i need to group with following P's.

I need to convert this-

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <row id="j1">
        <name>Marcus C.</name>
        <P id="j1_p1">[1] Text</P>
        <P id="j1_p2">[2] Text</P>
        <P id="j1_p3">[3] Text</P>
        <P id="j1_p4">[4] Text</P>
        <Head hlevel="h1">
            <Emph type="b">Facts</Emph>
        </Head>
        <P id="j1_p5">[5] Text</P>
        <P id="j1_p6">[6] Text</P>
        <P id="j1_p7">[7] Text</P>
        <Head hlevel="h1">
            <Emph type="b">Trial</Emph>
        </Head>
        <P id="j1_p8">[8] Text</P>
        <P id="j1_p9">[9] Text</P>
        <P id="j1_p10">[10] Text</P>
    </row>
</root>



Desired output would be :

<root>
    <row id="j1">
        <name>Marcus C.</name>
        <P id="j1_p1">[1] Text</P>
        <P id="j1_p2">[2] Text</P>
        <P id="j1_p3">[3] Text</P>
        <P id="j1_p4">[4] Text</P>
        <Group>
            <Head hlevel="h1">
                <Emph type="b">Facts</Emph>
            </Head>
            <P id="j1_p5">[5] Text</P>
            <P id="j1_p6">[6] Text</P>
            <P id="j1_p7">[7] Text</P>
        </Group>
        <Group>
            <Head hlevel="h1">
                <Emph type="b">Trial</Emph>
            </Head>
            <P id="j1_p8">[8] Text</P>
            <P id="j1_p9">[9] Text</P>
            <P id="j1_p10">[10] Text</P>
        </Group>
    </row>
</root>


Thanks.

What I have tried:

<xsl:template match="row/Head[child::Emph]">
   <Group>
       <Head>
           <xsl:apply-templates />
       </Head>
   </Group>
 </xsl:template>
Posted
Updated 27-Apr-17 19:50pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900