Rahul,
This will do what you want:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="/">
<xsl:result-document href="source-01-output.xml">
<xsl:apply-templates/>
</xsl:result-document>
</xsl:template>
<xsl:template match="country">
<xsl:copy>
<xsl:for-each-group select="*" group-by="city">
<xsl:element name="city">
<xsl:attribute name="type"
select="current-grouping-key()"/>
<xsl:copy-of select="current-group()[1]/pin"/>
<xsl:for-each select="current-group()">
<xsl:element name="zip">
<xsl:attribute name="type" select="zip"/>
<xsl:element name="phone">
<xsl:attribute name="type" select="phone"/>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Terry Badger
On Wednesday, June 12, 2019, 5:33:20 AM EDT, Rahul Singh
rahulsinghindia15(_at_)gmail(_dot_)com
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:
I need help for xsl.
Actually Grouping is not working in my xsl, Input and expected output are given
below.
Input xml:
<country>
<states>
<city>Albama</city>
<pin>1</pin>
<zip>aa</zip>
<phone>12</phone>
</states>
<states>
<city>Albama</city>
<pin>1</pin>
<zip>ab</zip>
<phone>11</phone>
</states>
<states>
<city>Albama</city>
<pin>1</pin>
<zip>ic</zip>
<phone>14</phone>
</states>
<states>
<city>Austrialia</city>
<pin>9</pin>
<zip>ac</zip>
<phone>04</phone>
</states>
<states>
<city>Canada</city>
<pin>8</pin>
<zip>pc</zip>
<phone>54</phone>
</states>
</country>
Expected Output XML:
<country>
<city type="Albama">
<pin type="1">
<zip type="aa">
<phone type="12"/>
</zip>
<zip type="ab">
<phone type="11"/>
</zip>
<zip type="ac">
<phone type="14"/>
</zip>
</pin>
</city>
<city type="Austrialia">
<pin type="9">
<zip type="ac">
<phone type="04"/>
</zip>
</pin>
</city>
<city type="Canada">
<pin type="8">
<zip type="pc">
<phone type="54"/>
</zip>
</pin>
</city>
</country>
XSL-List info and archive
EasyUnsubscribe (by email)
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--