How to convert flat xml data to hierarchical data xml



I have to convert a flat xml to hierarchical xml. I have no idea for this task. Below is input for conversion.


Input:-



<body>
<p class="title">Article Title</p>
<p class="Authors">abc, pqr and xyz</p>
<p class="intro">here is introdution text......</p>
<p class="head1">1: Heading level 1</p>
<p>some text here</p>
<p>some text here</p>
<p class="head2">1.1: Heading level 2</p>
<p>some text here</p>
<p>some text here</p>
<p class="head3">1.1.1: Heading level 3</p>
<p>some text here</p>
<p>some text here</p>
<p class="head1">2: Heading level 1</p>
<p class="head2">2.1: Heading level 2</p>
<p>some text here</p>
<p>some text here</p>
<p class="head3">2.1.1: Heading level 3</p>
<p>some text here</p>
<p>some text here</p>
<p class="head3">2.1.2: Heading level 3</p>
<p>some text here</p>
<p>some text here</p>
</body>


I want to convert it to below given xml.


Output:-



<article>
<title>Article Title</title>
<Authors>abc, pqr and xyz</Authors>
<introduction>here is introdution text......</introduction>
<body>
<sec level="1">
<title>1: Heading level 1</title>
<p>some text here</p>
<p>some text here</p>
<sec level="2">
<title>1.1: Heading level 2</title>
<p>some text here</p>
<p>some text here</p>
<sec level="3">
<title>1.1.1: Heading level 3</title>
<p>some text here</p>
<p>some text here</p>
</sec>
</sec>
</sec>
<sec level="1">
<title>2: Heading level 1</title>
<sec level="2">
<title>2.1: Heading level 2</title>
<p>some text here</p>
<p>some text here</p>
<sec level="3">
<title>2.1.1: Heading level 3</title>
<p>some text here</p>
<p>some text here</p>
</sec>
<sec level="3">
<title>2.1.2: Heading level 3</title>
<p>some text here</p>
<p>some text here</p>
</sec>
</sec>
</sec>
</body>
</article>


I havn't any idea to convert it using xslt.


No comments:

Post a Comment