Jump to content

[solved]Merging identical values using XSLT


karthikin

Recommended Posts

This is my xml file:

<?xml version="1.0" encoding="UTF-8"?><CONTACTS><CONTACT><FirstName>Arun</FirstName><LastName>Arun_Neelam</LastName><Email>nuraaa_iceee@yahoo.co.in</Email></CONTACT><CONTACT><FirstName>Arun</FirstName><LastName>Arun_Neelam</LastName><Email>nuraaa_iceee@gmail.com</Email></CONTACT></CONTACTS>

I want to have the output in the following way:

<?xml version="1.0" encoding="windows-1250"?><CONTACTS>    <CONTACT>        <FirstName>Arun</FirstName>        <LastName>Arun_Neelam</LastName>        <Email>nuraaa_iceee@gmail.com</Email>        <Email>nuraaa_iceee@yahoo.co.in</Email></CONTACT></CONTACTS>

How do i group them by checking two identical values of the <FirstName><LastName>, if it's true then take the email values and put it as a single contact.I'm not sure I can do it with current-group() and current-grouping-key().Thank you for your support.

Link to comment
Share on other sites

With XSLT 2.0 you can use

<xsl:template match="CONTACTS">  <xsl:copy>	<xsl:for-each-group select="CONTACT" group-by="concat(FirstName, '|', LastName)">		 <xsl:copy>			 <xsl:copy-of select="FirstName, LastName, current-group()/Email"/>		 </xsl:copy>	</xsl:for-each-group>  </xsl:copy></xsl:template>

With XSLT 1.0 you can use Muenchian grouping.

Link to comment
Share on other sites

With XSLT 2.0 you can use
<xsl:template match="CONTACTS">  <xsl:copy>	<xsl:for-each-group select="CONTACT" group-by="concat(FirstName, '|', LastName)">		 <xsl:copy>			 <xsl:copy-of select="FirstName, LastName, current-group()/Email"/>		 </xsl:copy>	</xsl:for-each-group>  </xsl:copy></xsl:template>

With XSLT 1.0 you can use Muenchian grouping.

Thanks for the solution.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...