This extends from http://stackoverflow.com/questions/2133459/xml-to-xml-using-xsl-problem I managed to import exslt and modified my codes according to the solution (thanks to Kyle Butt) given as follows:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:func="http://exslt.org/functions"
xmlns:set="http://exslt.org/sets"
extension-element-prefixes="set">
<xsl:import href="set.distinct.function.xsl"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="gallery">
<gallery>
<xsl:variable name="gallery" select="."/>
<xsl:for-each select="set:distinct(photo/tag/event)">
<xsl:value-of select="."/>
<event name="{.}">
<xsl:variable name="event" select="." />
<xsl:for-each select="set:distinct($gallery/object/tag[event=.]/group)">
<group name="{.}">
<xsl:variable name="group" select="." />
<xsl:apply-templates select="$gallery/object[tag/event=$event][tag/group=$group]" />
</group>
</xsl:for-each>
</event>
</xsl:for-each>
</gallery>
</xsl:template>
But theres error in the output which says- 'Function set:distinct() has failed. Value Cannot be null.' How to solve this?
Btw The XML input:
<gallery>
<photo>
<tag>
<event>Birthday</event>
<group>Family</group>
<other>Swensens</other>
</tag>
</photo>
<photo>..</photo>
</gallery>
& Required XML output:
<gallery>
<event name="Birthday">
<group name="Family">
<photo>
<tag>
<other>Swensens</other>
</tag>
</photo>
<photo>..</photo>
</group>
<group>..</group>
</event>
<event>..</event>
</gallery>