XSLT to print values of a node based on its position repeatively



I have to transform this sample xml in xslt 1.0 or xslt 2.0 Source XML



<root>
<group>
<spec>
<name>AAA1</name>
</spec>
<spec>
<name>AAA2</name>
</spec>
<spec>
<name>AAA3</name>
</spec>
</group>
<group>
<spec>
<name>BB1</name>
</spec>
<spec>
<name>BB2</name>
</spec>
<spec>
<name>BB3</name>
</spec>
</group>
</root>


Required XML OUTPUT



<?xml version="1.0" encoding="UTF-8"?>
<root>
<group>AAA1|BB1|AAA2|BB2|AAA3|BB3</group>
</root>


I tried the following xslt:


XSLT



<xsl:stylesheet version="1.0" xmlns:xsl="http://ift.tt/tCZ8VR">
<xsl:output method="xml" indent="yes" />
<xsl:template match="root">
<root>
<group>
<xsl:for-each select="group">
<xsl:for-each select="spec">
<xsl:choose>
<xsl:when test="position() != last()">
<xsl:value-of select="name" /><xsl:text>|</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="name" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:for-each>
</group>
</root>
</xsl:template>
</xsl:stylesheet>


Current-output



<?xml version="1.0" encoding="UTF-8"?>
<root>
<group>AAA1|AAA2|AAA3BB1|BB2|BB3</group>
</root>


How can I do this? Should I create a template a call it recursively?


No comments:

Post a Comment