p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   removing duplicate data in xml not displaying correct result (http://p2p.wrox.com/showthread.php?t=99863)

virgosaggi April 28th, 2018 10:31 AM

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

Martin Honnen April 28th, 2018 10:52 AM

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

virgosaggi April 28th, 2018 11:41 AM

Quote:

Originally Posted by Martin Honnen (Post 309912)
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

virgosaggi April 28th, 2018 11:57 AM

Quote:

Originally Posted by Martin Honnen (Post 309912)
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>

Martin Honnen April 28th, 2018 12:06 PM

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.

virgosaggi April 28th, 2018 12:17 PM

Quote:

Originally Posted by Martin Honnen (Post 309915)
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>

Martin Honnen April 28th, 2018 12:39 PM

I wanted you to change the other match pattern.

virgosaggi April 28th, 2018 12:52 PM

Quote:

Originally Posted by Martin Honnen (Post 309917)
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

virgosaggi April 28th, 2018 01:07 PM

Quote:

Originally Posted by Martin Honnen (Post 309917)
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

virgosaggi April 30th, 2018 07:47 AM

Quote:

Originally Posted by virgosaggi (Post 309919)
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


All times are GMT -4. The time now is 10:47 AM.

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