Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old April 28th, 2018, 10:31 AM
Registered User
Points: 110, Level: 2
Points: 110, Level: 2 Points: 110, Level: 2 Points: 110, Level: 2
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
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
Reply With Quote
  #2 (permalink)  
Old April 28th, 2018, 10:52 AM
Friend of Wrox
Points: 6,600, Level: 34
Points: 6,600, Level: 34 Points: 6,600, Level: 34 Points: 6,600, Level: 34
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,230
Thanks: 0
Thanked 241 Times in 240 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.
Reply With Quote
  #3 (permalink)  
Old April 28th, 2018, 11:41 AM
Registered User
Points: 110, Level: 2
Points: 110, Level: 2 Points: 110, Level: 2 Points: 110, Level: 2
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
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
Reply With Quote
  #4 (permalink)  
Old April 28th, 2018, 11:57 AM
Registered User
Points: 110, Level: 2
Points: 110, Level: 2 Points: 110, Level: 2 Points: 110, Level: 2
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
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.
Reply With Quote
  #5 (permalink)  
Old April 28th, 2018, 12:06 PM
Friend of Wrox
Points: 6,600, Level: 34
Points: 6,600, Level: 34 Points: 6,600, Level: 34 Points: 6,600, Level: 34
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,230
Thanks: 0
Thanked 241 Times in 240 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
Reply With Quote
  #6 (permalink)  
Old April 28th, 2018, 12:17 PM
Registered User
Points: 110, Level: 2
Points: 110, Level: 2 Points: 110, Level: 2 Points: 110, Level: 2
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
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>
Reply With Quote
  #7 (permalink)  
Old April 28th, 2018, 12:39 PM
Friend of Wrox
Points: 6,600, Level: 34
Points: 6,600, Level: 34 Points: 6,600, Level: 34 Points: 6,600, Level: 34
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,230
Thanks: 0
Thanked 241 Times in 240 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
Reply With Quote
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
Registered User
Points: 110, Level: 2
Points: 110, Level: 2 Points: 110, Level: 2 Points: 110, Level: 2
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
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
Reply With Quote
  #9 (permalink)  
Old April 28th, 2018, 01:07 PM
Registered User
Points: 110, Level: 2
Points: 110, Level: 2 Points: 110, Level: 2 Points: 110, Level: 2
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
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
Reply With Quote
  #10 (permalink)  
Old April 30th, 2018, 07:47 AM
Registered User
Points: 110, Level: 2
Points: 110, Level: 2 Points: 110, Level: 2 Points: 110, Level: 2
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 09:40 AM
Removing duplicate Value Row milindsaraswala Excel VBA 0 November 14th, 2007 05: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 09:16 PM



All times are GMT -4. The time now is 04:59 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.