XSLT grouping creation for sequence of elements with specfied condition

40 views Asked by At

I'm new for xslt i need to transform the xml to xml with grouping

My input is here below in that i need to group introduction related information that all are have to group with box element, and i have list blocks as para with specified a attribute i have to group that as list block

<p style="Chap"></p>
<p style="ChapTitle"></p>
<p style="IntroHead">IntroHead</p>
<p style="IntroText">IntroText</p>
<p style="IntroText">IntroText</p>
<p style="IntroText">IntroText</p>
<p style="IntroLast">IntroLast</p>
<p style="Normal"></p>

<p style="Bullet">Bullet</p>
<p style="Bullet">Bullet</p>
<p style="Bullet">Bullet</p>
<p style="Bullet">Bullet</p>
<p style="Bullet">Bullet</p>
<p style="BulletLast">BulletLast</p>
<p style="Normal"></p>
<p style="Normal"></p>
<p style="Normal"></p>
<p style="Normal"></p>
<p style="Normal"></p>
<p style="Normal"></p>
<p style="Normal"></p>
<p style="Normal"></p>

My expected output is

<p style="Chap"></p>
<p style="ChapTitle"></p>
<box>
<title>IntroHead</title>
<list>
<li>IntroText</li>
<li>IntroText</li>
<li>IntroText</li>
<li>IntroLast</li>
</list>
</box>
<p style="Normal"></p>

<list style="bullet">
<li>Bullet</li>
<li>Bullet</li>
<li>Bullet</li>
<li>Bullet</li>
<li>Bullet</li>
<li>BulletLast</li>
</list>
<p style="Normal"></p>
<p style="Normal"></p>
<p style="Normal"></p>
<p style="Normal"></p>
<p style="Normal"></p>
<p style="Normal"></p>
<p style="Normal"></p>
<p style="Normal"></p>
1

There are 1 answers

1
Michael Kay On

Use xsl:for-each-group/@group-adjacent to group paragraphs having the same @style. Within the body of the xsl:for-each-group do an xsl:choose: if current-grouping-key() is Introtext or bullet, wrap the group in a list element, otherwise copy it unchanged.