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 am just a beginner in XSL. I referred a lot online and tried to solve this problem. But i was failed. Somebody please help me to get the right xsl code for achieving the same. Please.
You may try something like this:
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="map" mode="concat" >
<xsl:param name="names" select="''" />
<xsl:variable name="newName" select="concat($names,@name)" />
<xsl:copy>
<xsl:attribute name ="name">
<xsl:value-of select="$newName"/>
</xsl:attribute>
<xsl:copy-of select="*[name() != 'map']"/>
</xsl:copy>
<xsl:apply-templates select="map" mode ="concat">
<xsl:with-param name="names" select="concat($newName,'+')" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="*" >
<xsl:copy>
<xsl:apply-templates select="map" />
</xsl:copy>
</xsl:template>
<xsl:template match="/*/map">
<xsl:apply-templates select="map" mode="concat">
<xsl:with-param name ="names" select="concat(@name,'+')" />
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
In this small example the mode my not be necessary but would be helpful if the xlst file grows. Otuput:
<?xml version="1.0"?>
<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>
</system>
Here's another 1.0 option...
XML Input
<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>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<system><xsl:apply-templates/></system>
</xsl:template>
<xsl:template match="map">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="map[*[not(self::map)]]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="name">
<xsl:for-each select="ancestor-or-self::map[@name]">
<xsl:if test="ancestor::map[@name]">+</xsl:if>
<xsl:value-of select="@name"/>
</xsl:for-each>
</xsl:attribute>
<xsl:apply-templates select="node()[not(self::map)]"/>
</xsl:copy>
<xsl:apply-templates select="map"/>
</xsl:template>
</xsl:stylesheet>
XML Output
<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>
</system>
Also, if you can use XSLT 2.0, you can replace the xsl:attribute
with this:
<xsl:attribute name="name" select="string-join(ancestor-or-self::map/@name,'+')"/>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.