INPUT XML:
<?xml version="1.0" encoding="ISO-8859-1"?> <system> <map name="Map"> <map name="Map1"> <color>Map1 is red colored</color> <shape>Map1 is square shaped</shape> </map> <map name="Map2"> <color>Map2 is green colored</color> <map name="Tap1"> <color>Tap1 is yellow colored</color> <speed>Tap1 is very fast</speed> </map> <map name="Tap2"> <speed>Tap2 is very slow</speed> </map> </map> </map> </system>
DESIRED OUTPUT:
<?xml version="1.0" encoding="ISO-8859-1"?> <system> <map name="Map+Map1"> <color>Map1 is red colored</color> <shape>Map1 is square shaped</shape> </map> <map name="Map+Map2"> <color>Map2 is green colored</color> </map> <map name="Map+Map2+Tap1"> <color>Tap1 is yellow colored</color> <speed>Tap1 is very fast</speed> </map> <map name="Map+Map2+Tap2"> <speed>Tap2 is very slow</speed> </map>
Current OUTPUT:
<system> <map name="Map1"/> <map name="Map1"/> <map name="Map2"/> <map name="Map2.Tap1"/> <map name="Map2.Tap1"/> <map name="Map2.Tap2"/> </system>
Current XSL stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="map[@name]"> <xsl:apply-templates mode="next"> <xsl:with-param name="name" select="''" /> </xsl:apply-templates> </xsl:template> <xsl:template match="map[@name]" mode="next"> <xsl:param name="name" /> <xsl:apply-templates mode="next"> <xsl:with-param name="name" select="concat($name,'.',@name)" /> </xsl:apply-templates> </xsl:template> <xsl:template match="*[not(*)]" mode="next"> <xsl:param name="name" /> <xsl:element name="map"> <xsl:attribute name="name"> <xsl:variable name="nameLength" select="string-length($name)-1"/> <xsl:value-of select="substring($name,2,$nameLength)"/> </xsl:attribute> </xsl:element> </xsl:template> <xsl:template match="@*|node()|*"> <xsl:copy> <xsl:apply-templates select="@*|node()|*"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
I'm just new to XSL. I talked a lot on the Internet and tried to solve this problem. But I failed. Someone please help me get the correct xsl code to achieve the same. You are welcome.
source share