In xslt transformation below is the input xml.
<root xmlns="test1">
<Entries xmlns="test2">
<root xmlns="test1">
<LAT>1</LAT>
</root>
<root xmlns="test1">
<LAT>2</LAT>
</root>
</Entries>
<Entries xmlns="test2">
<root xmlns="test1">
<LAT>3</LAT>
</root>
<root xmlns="test1">
<LAT>4</LAT>
</root>
</Entries>
</root>
The XSLT code i am using against this xml is.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:p="test1"
xmlns:r="test2"
version="1.0">
<xsl:template match="//*[local-name()='root']">
<xsl:element name="Test">
<xsl:for-each select="//p:root/r:Entries">
<xsl:variable name="i" select="position()"/>
<xsl:for-each select="//p:root/r:Entries/p:root">
<xsl:element name="{concat('imei', $i)}"> <xsl:element name="LAT"><xsl:value-of select="//*[local-name()='LAT']"/></xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
The result I wanted is :
<Test>
<imei1>
<LAT>1</LAT>
</imei1>
<imei1>
<LAT>2</LAT>
</imei1>
<imei2>
<LAT>3</LAT>
</imei2>
<imei4>
<LAT>4</LAT>
</imei4>
</Test>
The result I am getting is:
<Test>
<imei1>
<LAT>1</LAT>
</imei1>
<imei1>
<LAT>1</LAT>
</imei1>
<imei1>
<LAT>1</LAT>
</imei1>
<imei1>
<LAT>1</LAT>
</imei1>
<imei2>
<LAT>1</LAT>
</imei2>
<imei2>
<LAT>1</LAT>
</imei2>
<imei2>
<LAT>1</LAT>
</imei2>
<imei2>
<LAT>1</LAT>
</imei2>
The same value is repeating and looping twice, did i given the wrong xpath. The first loop is correctly executing twice as expected which we can see in the concatinated expression imei1 and imei2 is there but the second loop is executing twice the expected result count and giving same result.