Please observe robust XML below, and let me know if you need any more suggestions. :) I have provided more than what you had asked.
INPUT XML:
<?xml version="1.0" encoding="utf-8"?>
<content>
<first>Paragraph-1</first>
<likes>like-1</likes>
<first>Paragraph-2</first>
<comment>Comment-2</comment>
<likes>like-2</likes>
<first>Paragraph-3</first>
<comment>Comment-3</comment>
<first>Paragraph-4</first>
<comment>Comment-4</comment>
<likes>like-4</likes>
</content>
XSLT CODE:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="content">
<xsl:element name="content1">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<xsl:template match="first">
<xsl:variable name="count" select="count(following-sibling::first)"/>
<xsl:element name="block">
<aaa>
<xsl:apply-templates select="node()|@*"/>
</aaa>
<xsl:for-each select="following-sibling::comment[1][count(following-sibling::first) = $count]">
<bbb>
<xsl:apply-templates select="node()|@*"/>
</bbb>
</xsl:for-each>
<xsl:for-each select="following-sibling::likes[1][count(following-sibling::first) = $count]">
<ccc>
<xsl:apply-templates select="node()|@*"/>
</ccc>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template match="comment|likes"/>
</xsl:stylesheet>
Output XML:
<?xml version="1.0" encoding="utf-8"?>
<content1>
<block>
<aaa>Paragraph-1</aaa>
<ccc>like-1</ccc>
</block>
<block>
<aaa>Paragraph-2</aaa>
<bbb>Comment-2</bbb>
<ccc>like-2</ccc>
</block>
<block>
<aaa>Paragraph-3</aaa>
<bbb>Comment-3</bbb>
</block>
<block>
<aaa>Paragraph-4</aaa>
<bbb>Comment-4</bbb>
<ccc>like-4</ccc>
</block>
</content1>
This works like gem!!!!!! :D
<xsl:template match="content"><xsl:for-each-group select="*" group-starting-with="first">...</xsl:for-each-group>. I can post a complete example if you tell us that you can use XSLT 2.0.