Simplest approach (XSLT 1.0 is by far sufficient):
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:template match="/">
<start>
<xsl:apply-templates select="
A/B/C[position() mod 2 = 1 and following-sibling::C]"
/>
</start>
</xsl:template>
<xsl:template match="C">
<A>
<B>
<tag1>
<xsl:value-of select="text()" />
</tag1>
<tag2>
<xsl:value-of select="following-sibling::C[1]/text()" />
</tag2>
</B>
</A>
</xsl:template>
</xsl:stylesheet>
Also possible, maybe more flexible due to the use separate templates and <xsl:copy>
/<xsl:copy-of>
:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:template match="/">
<start>
<xsl:for-each select="
A/B/C[position() mod 2 = 1 and following-sibling::C]
">
<xsl:apply-templates select="ancestor::A">
<xsl:with-param name="C" select="." />
</xsl:apply-templates>
</xsl:for-each>
</start>
</xsl:template>
<xsl:template match="A">
<xsl:param name="C" />
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates select="B">
<xsl:with-param name="C" select="$C" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="B">
<xsl:param name="C" />
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates select="$C" />
</xsl:copy>
</xsl:template>
<xsl:template match="C">
<tag1>
<xsl:value-of select="text()" />
</tag1>
<tag2>
<xsl:value-of select="following-sibling::C[1]/text()" />
</tag2>
</xsl:template>
</xsl:stylesheet>
But then again, maybe this is over-complicated, it depends on your actual data. Key point is to select only the interesting <C>
nodes (those at odd positions) and build the rest of the transformation around them.
Both templates result in:
<start>
<A>
<B>
<tag1>hello</tag1>
<tag2>how</tag2>
</B>
</A>
<A>
<B>
<tag1>are</tag1>
<tag2>you</tag2>
</B>
</A>
</start>