Search code examples
xsltxslt-1.0xslt-2.0xslt-grouping

To merge two XMLs with common fields using XSLT


I have two xmls

  1. a.xml
  2. b.xml

Below is the a.xml file

<ConsumerKeyperUsers>

    <CONSUMERKEY_1>
        <CONSUMERKEY>QoChiX5FlNyqe1FuqU7VB2M</CONSUMERKEY>
    </CONSUMERKEY_1>
    <CONSUMERKEY_2>
        <CONSUMERKEY>uzGjRVxnxmP00zrnZPtB9sM</CONSUMERKEY>
    </CONSUMERKEY_2>
    <CONSUMERKEY_3>
        <CONSUMERKEY>RD37oO2jg4rlerxW5p6f76</CONSUMERKEY>
    </CONSUMERKEY_3>

</ConsumerKeyperUsers>

and below is the b.xml

<Details>

<Detail>
<ConsumeyKey>QoChiX5FlNyqe1FuqU7VB2M</ConsumeyKey>
<Place>Ocean</Place>
<City>Urban</City>
</Detail>

<Detail>
<ConsumeyKey>uzGjRVxnxmP00zrnZPtB9sM</ConsumeyKey>
<Place>Road</Place>
<City>Rural</City>
</Detail>

<Detail>
<ConsumeyKey>RD37oO2jg4rlerxW5p6f76</ConsumeyKey>
<Place>Plane</Place>
<City>Semiurban</City>
</Detail>

<Detail>
<ConsumeyKey>likujyhtasasa</ConsumeyKey>
<Place>Ship</Place>
<City>Semirural</City>
</Detail>

</Details>

Now, I want to get the output somewhat like below

    <ConsumerKeyperUsers>

    <CONSUMERKEY_1>
    <CONSUMERKEY>QoChiX5FlNyqe1FuqU7VB2M</CONSUMERKEY>
    <Place>Ocean</Place>
    <City>Urban</City>
    </CONSUMERKEY_1>

    <CONSUMERKEY_2>
    <CONSUMERKEY>uzGjRVxnxmP00zrnZPtB9sM</CONSUMERKEY>
    <Place>Road</Place>
    <City>Rural</City>
    </CONSUMERKEY_2>

    <CONSUMERKEY_3>
    <CONSUMERKEY>RD37oO2jg4rlerxW5p6f76</CONSUMERKEY>
    <Place>Plane</Place>
    <City>Semiurban</City>
    </CONSUMERKEY_3>

    </ConsumerKeyperUsers>

I am pretty new in xslt. Any xslt version would be ok to use.

Besides, I also want to know how to feed these two xml to the xslt. Do we have to store it in server where xslt can access these files.

Any guidance in this would be really helpful.

Thanks


Solution

  • I also want to know how to feed these two xml to the xslt.

    Tell your application to process the a.xml file, and pass the path to b.xml as a parameter.

    Use a key to lookup the data from b.xml - for example (untested):

    XSLT 2.0

    <xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:param name="file2" select="'b.xml'" />
    <xsl:key name="details" match="Detail" use="ConsumeyKey" />
    
    <xsl:template match="/ConsumerKeyperUsers">
        <xsl:copy>
            <xsl:for-each select="*">
                <xsl:copy>
                    <xsl:copy-of select="CONSUMERKEY"/>
                    <xsl:variable name="details" select="key('details', CONSUMERKEY, document($file2))" />
                    <xsl:copy-of select="$details/Place"/>
                    <xsl:copy-of select="$details/City"/>
                </xsl:copy>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

    To do the same thing using an XSLT 1.0 processor is slightly more complicated:

    XSLT 1.0

    <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"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:param name="file2" select="'b.xml'" />
    <xsl:key name="details" match="Detail" use="ConsumeyKey" />
    
    <xsl:template match="/ConsumerKeyperUsers">
        <xsl:copy>
            <xsl:for-each select="*">
                <xsl:copy>
                    <xsl:variable name="key" select="CONSUMERKEY" />
                    <xsl:copy-of select="$key"/>
                    <!--  switch context to file2 in order to use key -->
                    <xsl:for-each select="document($file2)">
                        <xsl:variable name="details" select="key('details', $key)" />
                        <xsl:copy-of select="$details/Place"/>
                        <xsl:copy-of select="$details/City"/>
                    </xsl:for-each> 
                </xsl:copy>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>