none
Avoid duplicate using XSLT in XML transfrmation RRS feed

  • Question

  • My XML is 

    <RxHistoryResponse>
                    <Response>
                        <Approved />
                    </Response>
                    <Pharmacy>
                        <Name>WALGREENS #12555</Name>
                        <Address>
                            <AddressLine1>2404 S PERRYVILLE RD</AddressLine1>
                            <City>ROCKFORD</City>
                            <State>IL</State>
                            <ZipCode>61108</ZipCode>
                        </Address>
                        <Identification>
                            <DEANumber />
                        </Identification>
                    </Pharmacy>
     </RxHistoryResponse>
     <RxHistoryResponse>
                    <Response>
                        <Approved />
                    </Response>
                    <Pharmacy>
                        <Name>WALGREENS #12555</Name>
                        <Address>
                            <AddressLine1>2404 S PERRYVILLE RD</AddressLine1>
                            <City>ROCKFORD</City>
                            <State>IL</State>
                            <ZipCode>61108</ZipCode>
                        </Address>
                        <Identification>
                            <DEANumber />
                        </Identification>
                    </Pharmacy>
     </RxHistoryResponse>
     <RxHistoryResponse>
                    <Response>
                        <Approved />
                    </Response>
                    <Pharmacy>
                        <Name>WALGREENS #2020</Name>
                        <Address>
                            <AddressLine1>1014 Lake st</AddressLine1>
                            <City>Chicago</City>
                            <State>IL</State>
                            <ZipCode>60403</ZipCode>
                        </Address>
                        <Identification>
                            <DEANumber />
                        </Identification>
                    </Pharmacy>
     </RxHistoryResponse>

    My out put should be 

    Pharmacy      Address                                          City       State 
    WALGREENS #12555 2404 S PERRYVILLE RD    ROCKFORD IL
    WALGREENS #2020 1014 Lake st Chicago IL

    The duplicate pharmacy info should not <g class="gr_ gr_40 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" data-gr-id="40" id="40">shown</g>.

    	   		 		
    	    	
    						





    Monday, March 11, 2019 8:38 AM

All replies

  • Hi sathyaonnet,

    Here we go, by using Muenchian's grouping methiod.

    XML (I made it well-formed):

    <?xml version="1.0"?>
    <root>
    	<RxHistoryResponse>
    		<Response>
    			<Approved/>
    		</Response>
    		<Pharmacy>
    			<Name>WALGREENS #12555</Name>
    			<Address>
    				<AddressLine1>2404 S PERRYVILLE RD</AddressLine1>
    				<City>ROCKFORD</City>
    				<State>IL</State>
    				<ZipCode>61108</ZipCode>
    			</Address>
    			<Identification>
    				<DEANumber/>
    			</Identification>
    		</Pharmacy>
    	</RxHistoryResponse>
    	<RxHistoryResponse>
    		<Response>
    			<Approved/>
    		</Response>
    		<Pharmacy>
    			<Name>WALGREENS #12555</Name>
    			<Address>
    				<AddressLine1>2404 S PERRYVILLE RD</AddressLine1>
    				<City>ROCKFORD</City>
    				<State>IL</State>
    				<ZipCode>61108</ZipCode>
    			</Address>
    			<Identification>
    				<DEANumber/>
    			</Identification>
    		</Pharmacy>
    	</RxHistoryResponse>
    	<RxHistoryResponse>
    		<Response>
    			<Approved/>
    		</Response>
    		<Pharmacy>
    			<Name>WALGREENS #2020</Name>
    			<Address>
    				<AddressLine1>1014 Lake st</AddressLine1>
    				<City>Chicago</City>
    				<State>IL</State>
    				<ZipCode>60403</ZipCode>
    			</Address>
    			<Identification>
    				<DEANumber/>
    			</Identification>
    		</Pharmacy>
    	</RxHistoryResponse>
    </root>

    XSLT:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:output method="xml" indent="yes"/>
    
    	<xsl:key name="uniquePharmacy" match="Pharmacy" use="Name"/>
    
    	<xsl:template match="/root">
    		<root>
    			<xsl:for-each select="RxHistoryResponse/Pharmacy[count(. | key('uniquePharmacy',Name)[1]) = 1]">
    				<row>
    					<Pharmacy>
    						<xsl:value-of select="Name"/>
    					</Pharmacy>
    					<Address>
    						<xsl:value-of select="Address/AddressLine1"/>
    					</Address>
    					<City>
    						<xsl:value-of select="Address/City"/>
    					</City>
    					<State>
    						<xsl:value-of select="Address/State"/>
    					</State>
    				</row>
    			</xsl:for-each>
    		</root>
    	</xsl:template>
    </xsl:stylesheet>

    Output:
    <?xml version="1.0" encoding="utf-8"?>
    <root>
      <row>
        <Pharmacy>WALGREENS #12555</Pharmacy>
        <Address>2404 S PERRYVILLE RD</Address>
        <City>ROCKFORD</City>
        <State>IL</State>
      </row>
      <row>
        <Pharmacy>WALGREENS #2020</Pharmacy>
        <Address>1014 Lake st</Address>
        <City>Chicago</City>
        <State>IL</State>
      </row>
    </root>

    Monday, March 11, 2019 2:53 PM