Given a source doc like so:
<A>
<B>
<C>item1</C>
</B>
<B>
<C>item2</C>
</B>
<B>
<C>item3</C>
</B>
</A>
What XSLT can I use to product something like this:
<A>
<B>
<C>item1</C>
<C>item2</C>
<C>item3</C>
</B>
</A>
I have tried a sheet with…
<xsl:template match="B">
<xsl:choose>
<xsl:when test="count(preceding-sibling::B)=0">
<B>
<xsl:apply-templates select="./C"/>
<xsl:apply-templates select="following-sibling::B"/>
</B>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="./C"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
but I am getting…
<A>
<B>
<C>item1</C>
<C>item2</C>
<C>item3</C>
</B>
<C>item2</C>
<C>item3</C>
</A>
A second question: I have a hard time debugging XSLT. hints?
Simplest approach:
To answer the question why you see the output you see with your XSLT:
I aussume that you have a
in place. This means:
<xsl:template match="B">is called three times, once for each<B>.<B>, it does what you intend, the other times it branches right into the<xsl:otherwise>, copying the<C>s via the<xsl:template match="C">you probably have. This is where your extra<C>s come from.…this: