Jump to content

[solved]Merging identical values using XSLT


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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...