Jump to content

[solved]Merging identical values using XSLT


karthikin
 Share

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.

Edited by karthikin
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...