Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 28th, 2018, 10:31 AM
Authorized User
Points: 110, Level: 2
Points: 110, Level: 2 Points: 110, Level: 2 Points: 110, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2018
Posts: 16
Thanks: 3
Thanked 0 Times in 0 Posts
Unhappy removing duplicate data in xml not displaying correct result

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="kMapping" match="Registration" use="Country"/>
<xsl:template match="@* | node()" name="Copy">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="Registration[generate-id() = generate-id(key('kMapping', Country)[1])]">
<xsl:call-template name="Copy"/>
</xsl:template>
<xsl:template match="Registration"/>
</xsl:stylesheet>

XML Code Input file
=================

<FundsXML> <Funds> <Fund> <Name>Fund11111 </Name> <FundFacts> <KeyFacts> <Registration> <Country>AT</Country> </Registration> <Registration> <Country>AT</Country> </Registration> <Registration> <Country>BE</Country> </Registration> <Registration> <Country>BE</Country> </Registration> <Registration> <Country>BE</Country> </Registration> </KeyFacts> </FundFacts> <Name>Fund22222 </Name> <SecurityCodes> <CountrySecurityCode> <CountryCode>DE</CountryCode> </CountrySecurityCode> </SecurityCodes> <FundFacts> <KeyFacts> <Registration> <Country>AE</Country> </Registration> <Registration> <Country>AT</Country> </Registration> <Registration> <Country>AT</Country> </Registration> <Registration> <Country>BE</Country> </Registration> <Registration> <Country>BE</Country> </Registration> <Registration> <Country>BE</Country> </Registration> <Registration> <Country>PT</Country> </Registration> </KeyFacts> </FundFacts> </Fund> </Funds> </FundsXML>

Desired output
Need country code to be displayed once per fund11111 and fund22222

<FundsXML> <Funds> <Fund> <Name>Fund11111 </Name> <FundFacts> <KeyFacts> <Registration> <Country>AT</Country> </Registration> <Registration> <Country>BE</Country> </Registration> </KeyFacts> </FundFacts> <Name>Fund22222 </Name> <FundFacts> <KeyFacts> <Registration> <Country>AE</Country> </Registration> <Registration> <Country>AT</Country> </Registration> <Registration> <Country>BE</Country> </Registration> <Registration> <Country>PT</Country> </Registration> </KeyFacts> </FundFacts> </Fund> </Funds> </FundsXML>
The code is displayed singlecountry record for first fund only and removing country value records which are present in Fund22222

Output displayed by above XSLT
==============================
<FundsXML> <Funds> <Fund> <Name>Fund11111 </Name> <FundFacts> <KeyFacts> <Registration> <Country>AT</Country> </Registration> <Registration> <Country>BE</Country> </Registration> </KeyFacts> </FundFacts> <Name>Fund22222 </Name> <FundFacts> <KeyFacts> <Registration> <Country>PT</Country> </Registration> </KeyFacts> </FundFacts> </Fund> </Funds> </FundsXML>


Any help will be much appreciated
  #2 (permalink)  
Old April 28th, 2018, 10:52 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Are you really restricted to XSLT 1.0?
In XSLT 2 you could use match="Registration[generate-id() = generate-id(key('kMapping', Country, ancestor::Fund)[1])]" or use for-each-group
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog

Last edited by Martin Honnen; April 28th, 2018 at 11:27 AM..
  #3 (permalink)  
Old April 28th, 2018, 11:41 AM
Authorized User
Points: 110, Level: 2
Points: 110, Level: 2 Points: 110, Level: 2 Points: 110, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2018
Posts: 16
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Martin Honnen View Post
Are you really restricted to XSLT 1.0?
In XSLT 2 you could use match="Registration[generate-id() = generate-id(key('kMapping', Country, ancestor::Fund)[1])]" or use for-each-group
Hi Martin

thanks for the information

Not really restricted with XSLT 1.0
Could use 2.0 version

Let me check on it and revert back

thank you
  #4 (permalink)  
Old April 28th, 2018, 11:57 AM
Authorized User
Points: 110, Level: 2
Points: 110, Level: 2 Points: 110, Level: 2 Points: 110, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2018
Posts: 16
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Martin Honnen View Post
Are you really restricted to XSLT 1.0?
In XSLT 2 you could use match="Registration[generate-id() = generate-id(key('kMapping', Country, ancestor::Fund)[1])]" or use for-each-group
---------------------------------
HI Martin

I have used following code for the xml filtering on duplicate. but out of 4 countries its displaying 2 countries which are duplicate per Fund

<FundFacts>
<KeyFacts>
<Registration>
<Country>AE</Country>
</Registration>
<Registration>
<Country>AE</Country>
</Registration>
<Registration>
<Country>AT</Country>
</Registration>
<Registration>
<Country>AT</Country>
</Registration>
<Registration>
<Country>TW</Country>
</Registration>
<Registration>
<Country>ZA</Country>
</Registration>
</KeyFacts>
</FundFacts>


<FundFacts>
<KeyFacts>
<Registration>
<Country>AE</Country>
</Registration>
<Registration>
<Country>AT</Country>
</Registration>
</KeyFacts>
</FundFacts>


<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" version="2.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="cc" match="Registration" use="Country"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="Registration[generate-id() = generate-id(key('cc', Country, ancestor::Fund)[1])]"
/>
</xsl:stylesheet>

Last edited by virgosaggi; April 28th, 2018 at 12:02 PM..
  #5 (permalink)  
Old April 28th, 2018, 12:06 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

My suggestion was to simply change that one match pattern to the one I posted, the rest of your code did not need any change but need to be kept. So don't change the key declaration and simply edit that match pattern as suggested.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
  #6 (permalink)  
Old April 28th, 2018, 12:17 PM
Authorized User
Points: 110, Level: 2
Points: 110, Level: 2 Points: 110, Level: 2 Points: 110, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2018
Posts: 16
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Martin Honnen View Post
My suggestion was to simply change that one match pattern to the one I posted, the rest of your code did not need any change but need to be kept. So don't change the key declaration and simply edit that match pattern as suggested.
Hi Martin

All I did based on suggestion was below code nothing else changed

<xsl:template match="Registration[generate-id() = generate-id(key('cc', Country, ancestor::Fund)[1])]"
/>
</xsl:stylesheet>


Sample Data:
================

<FundsXML>
<Funds>
<Fund>
<Name>Fund111111</Name>
<FundFacts>
<KeyFacts>
<Registration>
<Country>AE</Country>
</Registration>
<Registration>
<Country>AE</Country>
</Registration>
<Registration>
<Country>AT</Country>
</Registration>
<Registration>
<Country>AT</Country>
</Registration>
<Registration>
<Country>TW</Country>
</Registration>
<Registration>
<Country>ZA</Country>
</Registration>
</KeyFacts>
</FundFacts>
</Fund>
</Funds>
<Fund>
<Name>Fund222222</Name>
<FundFacts>
<KeyFacts>
<Registration>
<Country>AE</Country>
</Registration>
<Registration>
<Country>AE</Country>
</Registration>
<Registration>
<Country>IT</Country>
</Registration>
<Registration>
<Country>IT</Country>
</Registration>
<Registration>
<Country>TA</Country>
</Registration>
<Registration>
<Country>ZA</Country>
</Registration>
</KeyFacts>
</FundFacts>
</Fund>
</FundsXML>
  #7 (permalink)  
Old April 28th, 2018, 12:39 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I wanted you to change the other match pattern.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
virgosaggi (April 28th, 2018)
  #8 (permalink)  
Old April 28th, 2018, 12:52 PM
Authorized User
Points: 110, Level: 2
Points: 110, Level: 2 Points: 110, Level: 2 Points: 110, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2018
Posts: 16
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Martin Honnen View Post
I wanted you to change the other match pattern.
Hello Martin

Fund is the main pattern to filter out the distinct country code fund wise

thank you
  #9 (permalink)  
Old April 28th, 2018, 01:07 PM
Authorized User
Points: 110, Level: 2
Points: 110, Level: 2 Points: 110, Level: 2 Points: 110, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2018
Posts: 16
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Martin Honnen View Post
I wanted you to change the other match pattern.
Hi Martin

It seem to work with the suggestion you gave

I added NOT clause

<xsl:template match="Registration[not( generate-id() = generate-id(key('cc', Country, ancestor::Fund)[1]))]"
/>
</xsl:stylesheet>

checking on dataset
  #10 (permalink)  
Old April 30th, 2018, 07:47 AM
Authorized User
Points: 110, Level: 2
Points: 110, Level: 2 Points: 110, Level: 2 Points: 110, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2018
Posts: 16
Thanks: 3
Thanked 0 Times in 0 Posts
Unhappy

Quote:
Originally Posted by virgosaggi View Post
Hi Martin

It seem to work with the suggestion you gave

I added NOT clause

<xsl:template match="Registration[not( generate-id() = generate-id(key('cc', Country, ancestor::Fund)[1]))]"
/>
</xsl:stylesheet>

checking on dataset
Hi Martin
Using the suggestions above it worked in xmlspy
But when I am trying to run based on a wrapper code it’s failing saying y needs two arguments to the generate-id(key())

Any suggestions

Thank you




Similar Threads
Thread Thread Starter Forum Replies Last Post
removing duplicate in xmlnotdisplaying correct result virgosaggi XSLT 0 April 28th, 2018 10:30 AM
removing duplicate nodes nguna XSLT 6 February 16th, 2009 10:40 AM
Removing duplicate Value Row milindsaraswala Excel VBA 0 November 14th, 2007 06:11 PM
Removing the Duplicate anukagni Access 3 August 31st, 2006 12:57 AM
Avoiding Displaying duplicate data saleaf10 Classic ASP Databases 1 November 26th, 2003 10:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.